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

Variadic template argument order, must they always be the right most argument?

I would like to modify an existing class constructor: template< typename T, typename... Ts > MyClass( std::vector& head, Ts& ...tail ); So that a processing flag can be specified: template< typename T, typename... Ts > MyClass( MyEnum myEnum,…
kbirk
  • 3,906
  • 7
  • 48
  • 72
16
votes
2 answers

Why is an ellipsis preferred to a variadic template when called with no arguments?

I'm using the following SFINAE pattern to evaluate a predicate on a variadic type list: #include void f(int = 0); // for example template()...))> std::true_type check(T…
ecatmur
  • 152,476
  • 27
  • 293
  • 366
16
votes
2 answers

How to create a type list (for variadic templates) that contains n-times the same type?

I would like my class template class X; to create a std::tuple that contains n times the type T. Is there a particularly neat way for this? Is there also a nice way to do this for arbitrary variadic template classes? This…
Markus Mayr
  • 4,038
  • 1
  • 20
  • 42
16
votes
1 answer

How does template argument deduction work in this case?

Given this code, how does template argument deduction decide what to do for the last function call? #include template Ret foo(Args&&...) { std::cout << "not void\n"; return…
chris
  • 60,560
  • 13
  • 143
  • 205
16
votes
6 answers

Is there a way to write make_unique() in VS2012?

Herb Sutter propose a simple implementation of make_unique() there: http://herbsutter.com/gotw/_102/ Here it is: template std::unique_ptr make_unique( Args&& ...args ) { return std::unique_ptr( new T(…
Klaim
  • 67,274
  • 36
  • 133
  • 188
16
votes
4 answers

Easiest way to get the N-th argument of a variadic templated class?

I wonder what is the easiest and more common way to get the N-th parameter of a variadic templated class at compile-time (The returned value has to be as a static const for the compiler in order to do some optimizations). Here is the form of my…
Vincent
  • 57,703
  • 61
  • 205
  • 388
16
votes
2 answers

Variadic templates with 'const' parameter overloading

Reduced sample code: #include template void func(T &x) { std::cout << "non-const " << x << std::endl; } template void func(const T &x) { std::cout << "const " << x << std::endl; } template
user1346466
  • 630
  • 4
  • 11
15
votes
2 answers

How to perfect forward variadic template args with default argument std::source_location?

I want to track where the creation of an object occurs using std::source_location. The following is a simplified example without the extra code to account for copy and movement of a Tracked object. #include #include #include…
15
votes
2 answers

Variadic template works in gcc but not in clang

I'm learning C++ using the books listed here. In particular I read about variadic templates. Now, to further clear my concepts I'm also writing simple examples and trying to understand them by myself using debugger and cout statements. One such…
Alex
  • 318
  • 1
  • 14
15
votes
1 answer

Variadic Templates Multidimensional Array Container

In the C++0x Variadic Templates Proposal paper Link there is an example of a class which supports an arbitrary number of dimensions. I have copied it below: template class array { /*…
Ricky65
  • 1,657
  • 18
  • 22
15
votes
2 answers

Which part of the C++ standard prevents explicitly specifying this template's arguments?

In my C++ travels I've come across the following idiom (for example here in Abseil) for ensuring that a templated function can't have template arguments explicitly specified, so they aren't part of the guaranteed API and are free to change without…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
15
votes
3 answers

Problem with calling a variadic function template when passing brace initialiser list arguments

Consider this function template: template void foo (std::tuple ... x); This invocation works: using K = std::tuple; foo ( K{1,'2',3.0}, K{4,'5',6.0}, K{7,'8',9.0} ); This one doesn't: foo (…
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
15
votes
3 answers

Use a enable_if that does not depend on method's template parameter

I'm trying to use std::enable_if and SFINAE to switch out the implementation of a class template's method based purely on the template parameters of the class. Example: #include template class Foo { …
Lukas Barth
  • 2,734
  • 18
  • 43
15
votes
1 answer

efficient index computation using meta programming

Given an multi-dimensional array with shape [A][B][C][D] but stored as a 1-dim array with length [A*B*C*D]. I want to use template meta-programming to simplify the index-computation. The index (a,b,c,d) should be at position a*B*C*D + b*C*D + c*D +…
Patwie
  • 4,360
  • 1
  • 21
  • 41
15
votes
5 answers

Meta-iteration over variadic templates arguments

I would like to generalize the following pattern: template class Foo { protected: template void foo(const T& t) {...do stuff...} public: void bar(const A1& a) { foo(a); } void bar(const A2& a) { foo(a);…
user3612643
  • 5,096
  • 7
  • 34
  • 55