Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7681539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:21:52+00:00 2026-05-31T18:21:52+00:00

I want to use type-safe variadic functions introduced in C++11, but not with different

  • 0

I want to use type-safe variadic functions introduced in C++11, but not with different types.
A example:

template<typename T>
T maxv(T first, T second) {
  return first > second ? first : second; 
}

template<typename T, typename ... Rest>
T maxv(T first, T second, T ... rest) {
  return maxv(first, maxv(second, rest));
}

The type of all parameters are the same, so it is maybe possible to write something like that:

struct Point { int x,y; };  

template<>
Point maxv(Point first, Point second) {
  return first.x > second.x ? first : second; 
}  

maxv({1, 2}, {3, 4});         // no problem    
maxv({1, 2}, {3, 4}, {5, 6}); // compile error

It compiles with this error in mingw g++4.5:

error: no matching function for call to 'maxv(<brace-enclosed initializer list>, <brace-enclosed initializer list>, <brace-enclosed initializer list>)'

Because he do not know that {5, 6} is of type Point. What is the solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:21:53+00:00Added an answer on May 31, 2026 at 6:21 pm

    The solution is not to use variadic templates! When used with function templates they are intended to deduce the types of the arguments. That is not what you want to do: you want the arguments to take on the expected type.

    I don’t have a lot of practical experience with this but you’d want to use initializer lists for this:

    Point maxv(std::initializer_list<Point> list) {
        ...
    }
    

    You might complain that you can’t use this with arbitrary types but then you need to realize that you need to tell somewhere what type are involved. … And it can be made a template although you’d need to specify the argument type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On a web project for a client, we want to use a type of
I am use next type of strings: LPCSTR, TCHAR, String i want to convert:
I want to serialize an object of type Person . I want to use
Ok bit of a newbie type question. I want to use Core Data, together
I've used mime_content_type() and File info but i never successed. i want to use
TNumberbox and TSpinEdit return values defined as type single . I want to use
In the code below, I don't want to use instanceof to know the type
For debug use I want to show the type of a variable in Google
Possible Duplicate: C++ variable types limits I have a defined type that may not
These functions are Utility type things that most of my programs objects will use.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.