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
28
votes
4 answers

C++ variadic template function parameter with default value

I have a function which takes one parameter with a default value. Now I also want it to take a variable number of parameters and forward them to some other function. Function parameters with default value have to be last, so... can I put that…
27
votes
3 answers

Create static array with variadic templates

There was an answer on stackoverflow (which I can't seem to find anymore) which demonstrated how a variadic template can be used in C++11 to create a static array at compile time: template struct array_ { static const T…
Channel72
  • 24,139
  • 32
  • 108
  • 180
27
votes
1 answer

When are template parameter packs deduced as empty?

Consider the following examples (Coliru link): template struct S { using type = int; }; template void f(typename S::type) { static_assert(sizeof...(T) == 0); } template void g(typename…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
27
votes
1 answer

What is the best way of renaming (alias/forward) a function in C++?

(I'll restrict this question to C++11, since I believe there is no general way to do this in C++98). Supposed I have a complicated (in terms of signature) set of template functions and/or overloaded functions, and I want to use these functions in…
alfC
  • 14,261
  • 4
  • 67
  • 118
26
votes
3 answers

How can I create a n way Cartesian product of type lists in C++?

Self explanatory. Basically, say I have type lists like so: using type_list_1 = type_list; using type_list_2 = type_list; using type_list_3 = type_list; They can be variadic number of type lists. How do…
themagicalyang
  • 2,493
  • 14
  • 21
26
votes
5 answers

split variadic template arguments

How do I split variadic template arguments in two halves? Something like: template struct a { std::array p, q; template a (T ... t) : p ({half of t...}), q ({other half of t...}) {} };
Thomas
  • 325
  • 3
  • 6
26
votes
7 answers

C++17 Variadic Template Folding

I don't understand why this doesn't work. Could someone who understands templates and variadic expression folding explain what is going on and give a solution that does work? #include #include template void…
nickeb96
  • 789
  • 1
  • 10
  • 16
26
votes
7 answers

C++ parameter pack, constrained to have instances of a single type?

Since C++11 we can make template functions which can accept any sequence of arguments: template void func(Ts &&... ts) { step_one(std::forward(ts)...); step_two(std::forward(ts)...); } However, suppose that it really…
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
26
votes
4 answers

Writing variadic template constructor

Recently I asked this question but now I would like to expand it. I wrote the following class: template class X{ public: vector v; template X(T n) { v.push_back(n); } template
Tracer
  • 2,544
  • 2
  • 23
  • 58
26
votes
2 answers

Can a parameter pack be captured implicitly within C++11 lambdas?

Does anyone know if the following implicit capture of 'ts' is well-formed: template void bar(Ts ... ts) { } template int foo(Ts ... ts) { auto L = [=] () { bar(ts...); }; L(); return 0; } int g =…
Faisal Vali
  • 32,723
  • 8
  • 42
  • 45
25
votes
1 answer

Code not compiling when template functions are placed in a certain order

The following program compiles successfully. template T sum(T x) { return x; } template T sum(T x, Args... args) { return x + sum(args...); } int main() { sum(1, 2, 3, 4, 5); } However,…
Ricardo
  • 355
  • 2
  • 5
25
votes
6 answers

Get first N elements of parameter pack

I have to following problem: template< size_t... N_i > class A { // ... }; template< size_t N, size_t... N_i > A foo() { A a; // ... return a; } int main() { A<1,2> res…
abraham_hilbert
  • 2,221
  • 1
  • 13
  • 30
25
votes
5 answers

check variadic templates parameters for uniqueness

I want variadic template parameters must unique. I know when multi inheritance, identical classes inheritance is not allowed. struct A{}; struct B: A, A{}; // error Using this rule, I made a little code. #include template< class…
Khurshid Normuradov
  • 1,564
  • 1
  • 13
  • 20
25
votes
2 answers

Splitting argpack in half?

How can I split an argument pack in two equal parts? For example, I would like to do something like this: template T sum(const T& t) { return t; } template T sum(const T& t1, const T& t2) { return t1 + t2;…
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
24
votes
5 answers

Variadic templates and switch statement?

I have the following function which can take N arguments of different types, and forwards them to N functions templated on each individual type, in this manner (example with two arguments): template bool func(int& counter,…
Thomas
  • 3,321
  • 1
  • 21
  • 44