Questions tagged [variadic-templates]

Variadic templates are templates that take a variable number of parameters.

Some programming languages, like D and C++ since with the C++11 standard, support templates that take a variable number of parameters. Variadic templates are useful in a number of situations, for example, defining type-safe heterogeneous containers such as tuples and expanded metaprogramming library facilities.

http://en.wikipedia.org/wiki/Variadic_templates

3928 questions
13
votes
6 answers

sizeof variadic template (sum of sizeof of all elements)

Considering the following function : template inline unsigned int myFunction(const List&... list) { return /* SOMETHING */; } What is the most simple thing to put instead of /* SOMETHING */ in order to return the sum of…
Vincent
  • 57,703
  • 61
  • 205
  • 388
13
votes
1 answer

binding member functions in a variadic fashion

I have a member function with a variable number of parameters, stored in a std::function, and I want to bind the instance and get an independent function object. template void connect(const T& t,…
lucas clemente
  • 6,255
  • 8
  • 41
  • 61
13
votes
2 answers

How do I get the copy constructor called over a variadic constructor?

In the following code, the variadic constructor is called twice. How can I get the copy constructor to be called instead of the single argument version of the variadic constructor when appropriate? #include struct Foo { Foo(const…
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
12
votes
3 answers

Check at Compile-Time if Template Argument is void

I'm trying to wrap the Windows API functions to check errors when I so choose. As I found out in a previous SO question, I could use a template function to call the API function, and then call GetLastError() to retrieve any error it might have set.…
chris
  • 60,560
  • 13
  • 143
  • 205
12
votes
1 answer

Variadic Templates, Perfect Forwarding to functions with default arguments

I have been using a variadic template that acts as an exception firewall in an interface between C and C++. The template simply takes a function, followed by N arguments and calls the function inside a try catch block. This has been working fine,…
mark
  • 7,381
  • 5
  • 36
  • 61
12
votes
4 answers

Compile-time C++ function to check whether all template argument types are unique

There is a nice question (Which substitution failures are not allowed in requires clauses?) proposing the next problem. One needs to write a compile-time function template constexpr bool allTypesUnique() that will return true if all…
Fedor
  • 17,146
  • 13
  • 40
  • 131
12
votes
2 answers

How to implement folding with variadic templates

I have an almost working solution. However, it fails to compile some simple cases, and I can't decipher the error message. My current solution: #define AUTO_RETURN( EXPR ) -> decltype( EXPR ) \ { return EXPR; } template< typename BinaryFunc,…
deft_code
  • 57,255
  • 29
  • 141
  • 224
12
votes
1 answer

Handle variadic templates for general-case mixed types and non-types

So I'm trying to make a type trait that says whether two "outer" class types are the same. ie. std::vector is the same as std::vector, I don't care about any inner arguments for my type trait. A problem that I had with trying to make a…
12
votes
3 answers

How can a variadic template be used to generate a left-associative expression (aka left fold) in c++11?

I would like to use a c++ template to aggregate (fold) multiple arguments using a binary operation. Such a template could be used as follows: fold(100,10,5) expands to add(add(100, 10), 5) The particular expansion shown above is the "left…
drwatsoncode
  • 4,721
  • 1
  • 31
  • 45
12
votes
3 answers

Concatenating tuples as types

I'm trying to practice some template programming. Maybe there's a standard way to do this, and I would be thankful for such answers, but my main goal is to practice the template programming techniques, so I tried to implement it myself: I need to…
egst
  • 1,605
  • 3
  • 18
  • 25
12
votes
4 answers

Make C++14 constexpr function C++11 compatible

I have written a class multi_array which is sort of an extension of std::array to multiple dimensions. template class multi_array { template constexpr std::size_t…
12
votes
2 answers

Could not convert from brace-enclosed initializer list to std tuple

As part of a bigger project, I'm playing with std::tuple and templates; consider the following code: template void foo(tuple t) {} void bar(tuple t) {} tuple quxx() { return {1, 'S'}; } int main(int argc,…
12
votes
1 answer

g++ and clang++ different behaviour with pointer to variadic template functions

Another "who's right between g++ and clang++ ?" question for C++ standard gurus. The code is the following template struct bar { }; template void foo (bar const &) { } int main () { foo(bar
max66
  • 65,235
  • 10
  • 71
  • 111
12
votes
6 answers

Using variable number of float arguments

I'm trying to implement a flexible constructor for my struct Polynomial : struct Polynomial { std::vector coefficients; size_t degree; }; The degree of the polynomial is variable. What I would like is to have a constructor like…
e.farman
  • 335
  • 2
  • 12
12
votes
3 answers

Test if all elements are equal with C++17 fold-expression

I have a function taking a variadic parameter pack and at the beginning I want to check that all elements compare equal. Can I somehow use the new C++17 fold-expressions to write that succinctly as a one-liner? I was thinking template
levzettelin
  • 2,600
  • 19
  • 32