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
20
votes
5 answers

how to avoid undefined execution order for the constructors when using std::make_tuple

How can I use std::make_tuple if the execution order of the constructors is important? For example I guess the execution order of the constructor of class A and the constructor of class B is undefined for: std::tuple
Erik Sjölund
  • 10,690
  • 7
  • 46
  • 74
19
votes
2 answers

Necessity of forward-declaring template functions

I recently created this example code to illustrate C++11 variadic template function usage. template void foo (Head, Tail...); template void foo (int, Tail...); void foo () {} template…
spraff
  • 32,570
  • 22
  • 121
  • 229
19
votes
2 answers

Which is the more specialized template function? clang and g++ differ on that

While playing with variadic templates, following this SO question (note: it is not mandatory to go there for following this question), I came to a different behavior of clang (3.8) and g++ (6.1) for the following template overloaded…
Amir Kirsh
  • 12,564
  • 41
  • 74
19
votes
2 answers

Tuple to parameter pack

This below code from user Faheem Mitha, is based on user Johannes Schaub - litb's answer in this SO. This code perfectly does what I seek, which is conversion of a tuple into parameter pack, but I don't understand this code well enough and therefore…
DigitalEye
  • 1,456
  • 3
  • 17
  • 26
19
votes
6 answers

How can I use C++11 variadic templates to define a vector-of-tuples backed by a tuple-of-vectors?

Suppose I have a bunch of vectors: vector v1; vector v2; vector v3; all of the same length. Now, for every index i, I would like to be able to treat (v1[i], v2[i], v3[i]) as a tuple, and maybe pass it around. In fact, I want to…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
19
votes
1 answer

Scalability of variadic templates

I am working on a large-scale software infrastructure in C++11 that makes extensive use of variadic templates. My question is the following: what is the scalability of this approach? First, is there an upper limit in the number of arguments that…
Greg
  • 6,038
  • 4
  • 22
  • 37
19
votes
6 answers

How do I reverse the order of element types in a tuple type?

How do I reverse the types in a tuple? For example, I want reverse_tuple>::type to be std::tuple. I tried doing the following but it didn't work. What did I do wrong? #include #include…
Me myself and I
  • 3,990
  • 1
  • 23
  • 47
19
votes
5 answers

How do I strip a tuple<> back into a variadic template list of types?

Is there a way to strip a std::tuple in order to get it back to T...? Example Suppose vct is a pre-existing variadic class template, using U = std::tuple; using X = vct; using Y = vct< strip
kfmfe04
  • 14,936
  • 14
  • 74
  • 140
19
votes
2 answers

A function with variable number of arguments with known types, the c++11 way

I already know the stdarg.h way to have a function with variable arguments in c++ as discussed here for example. I also know c++11 standard has variadic templates as explained here. But in both of aforementioned schemes we don't know (and we can't…
melmi
  • 988
  • 3
  • 9
  • 27
18
votes
9 answers

How to create the Cartesian product of a type list?

I'd like to create the cross product of a list of types using variadic templates. Here's what I have so far: #include #include #include template struct type_list {}; template
Jared Hoberock
  • 11,118
  • 3
  • 40
  • 76
18
votes
5 answers

How to make_from_tuple on the heap?

So in C++ there is now make_from_tuple as: T obj = std::make_from_tuple( { Args... args } ); // args represents a tuple but how would one do: T* obj = std::make_new_from_tuple( { Args... args } ); There is make_shared and make_unique but…
CashCow
  • 30,981
  • 5
  • 61
  • 92
18
votes
1 answer

Lambda pack capture with ellipsis on both sides - what is the meaning?

P0780 ("Allow pack expansion in lambda init-capture"), approved for C++20, allows to generate a pack of closure data members by placing an ellipsis (...) before a pack expansion as part of a lambda capture. This is useful - for example - when…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
18
votes
1 answer

Class template specialization priority/ambiguity

While trying to implement a few things relying on variadic templates, I stumbled accross something I cannot explain. I boiled down the problem to the following code snippet: template struct A {}; template