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
22
votes
2 answers

Why won't template parameter pack be deduced to multiple type arguments in function call?

I have a class templated on a type parameter and parameter pack, and am confused about type-deduction of this type; while writing an output-streaming operator I discovered a parameter pack on operator<< will not match both the type and pack…
boycy
  • 1,473
  • 12
  • 25
22
votes
1 answer

Variadic template as template parameter: deduction works with GCC but not with Clang

While compiling some C++11 code with both GCC 4.7.2 and Clang 3.1, I ran into a problem with Clang not managing to deduce a template argument where GCC succeeds. In a more abstract form, the code looks like this: src/test.cc: struct Element…
psyill
  • 891
  • 6
  • 10
21
votes
1 answer

Variadic variadic template templates

I'm currently struggling with the following code, the intent of which is to implement variadic variadic template templates: template < template class Head, template class... > struct…
kmore
  • 924
  • 9
  • 18
21
votes
2 answers

variadic template parameter pack expanding for function calls

I am looking for something like that: template< typename T> void func(T t) { } template< typename... Parms> void anyFunc( Parms... p) { func(p)... ; //error func(p)... ; //error } If the parameter pack expansion is…
Klaus
  • 24,205
  • 7
  • 58
  • 113
21
votes
9 answers

Check if one set of types is a subset of the other

How can one check if one parameter pack (interpreted as a set) is a subset of another one? So far I only have the frame (using std::tuple), but no functionality. #include #include template struct…
21
votes
1 answer

Strange behaviour of default template-argument in a template-template-parameter

Consider this C++11 program: #include template struct Cont { Cont () { std::cout << sizeof(B); } }; template class C, class E> class Wrap1 { C ce; }; template…
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
21
votes
5 answers

Enforce variadic template of certain type

I would like to enforce the type of variadic template to be identical to an earlier set template type. In the below example, I'd like T and U to be the same type. code on ideone.com #include #include template struct Foo…
Gerard
  • 831
  • 6
  • 15
21
votes
1 answer

Why is std::make_tuple(7 + N...) legal in C++11?

The following code is legal in C++11. template std::tuple f() { return std::make_tuple(7 + N...); } What does it mean?
xmllmx
  • 39,765
  • 26
  • 162
  • 323
21
votes
3 answers

iterating over variadic template's type parameters

I have a function template like this: template do_something() { // i'd like to do something to each A::var, where var has static storage } I can't use Boost.MPL. Can you please show how to do this without recursion? EDIT: These days…
user1095108
  • 14,119
  • 9
  • 58
  • 116
21
votes
4 answers

Variadic function template with pack expansion not in last parameter

I am wondering why the following code doesn't compile: struct S { template S(T..., int); }; S c{0, 0}; This code fails to compile with both clang and GCC 4.8. Here is the error with clang: test.cpp:7:3: error: no matching…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
20
votes
1 answer

Template specialization with variadic templates

template void doStuff(Params...) { } template <> void doStuff(int, bool) { } int main(int, char**) { doStuff<1,int,bool>(1, false); return 0; } This doesn't compile, the second…
coyotte508
  • 9,175
  • 6
  • 44
  • 63
20
votes
2 answers

The variadic template constructor of my class cannot modify my class members, why is that so?

I've been working on a task we got by our professor, where I have to work around a variadic template class. The problem is, I cannot modify the class members within the recursive constructor. I cannot figure out why this is the case, as soon as it…
PrettyCoffee
  • 201
  • 1
  • 4
20
votes
5 answers

Parameter packs not expanded with '...'

I have this code: #include using namespace std; int print(int i) { cout << endl << i; } template inline void pass(Args&&...args) { } template inline void expand(args&&... a) { print(a) ...;…
q126y
  • 1,589
  • 4
  • 18
  • 50
20
votes
2 answers

C++ std::function-like template syntax

In C++11 you can instantiate std::function like this: std::function f1; std::function f2; //and so on But while there is plenty of info on variadic templates on the web, I fail to find any articles on how…
Smiles
  • 1,733
  • 2
  • 11
  • 13
20
votes
1 answer

Is it possible to typedef a parameter pack?

Is it possible to typedef a parameter pack? For example template struct A { typedef T Type; // We typedef it, then its derived class can use it. // How about for parameter packs? // Option 1: …
user1899020
  • 13,167
  • 21
  • 79
  • 154