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
11
votes
3 answers

Class with types dependant on variadic templating

I recently watched a video which inspired me to write my own neural network system, and I wanted to have the amount of nodes in the network be adjustable. At first I achieved this at runtime by parsing an array of the numbers of nodes but I was…
11
votes
2 answers

How to define a tuple of value types from a parameter pack

I need to build a tuple of n types. These n types are value types of n other types. Consider this snippet: #include namespace hana = boost::hana; template class CartesianProduct { public: …
lo tolmencre
  • 3,804
  • 3
  • 30
  • 60
11
votes
2 answers

Which compiler, if any has a bug in parameter pack expansion?

When experimenting with convenient ways to access tuples as containers, I wrote a test program. on clang (3.9.1, and apple clang) it compiles as expected, producing the expected output: 1.1 foo 2 on gcc (5.4, 6.3), it fails to compile: : In…
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
11
votes
5 answers

Compare two sets of types for equality

How can one check if two parameter packs are the same, ignoring their internal order? So far I only have the frame (using std::tuple), but no functionality. #include #include template struct type_set_eq :…
11
votes
2 answers

Applying multiple tuples to the same function (i.e. `apply(f, tuples...)`) without recursion or `tuple_cat`

std::experimental::apply has the following signature: template constexpr decltype(auto) apply(F&& f, Tuple&& t); It basically invokes f by expanding t's elements as the arguments. I would like something that does the exact…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
11
votes
3 answers

How to overload variadic templates when they're not the last argument

Basically the problem can be summed up with this example: template void foo(Us...) { std::cout << "A\n"; } template void foo(Us..., int) { std::cout << "B\n"; } int main(){ foo(1,2,3); } This calls the first…
JoshG79
  • 1,685
  • 11
  • 14
11
votes
1 answer

How to implement an abstract method when abstract class is used in a variadic context

How to implement in the following code the abstract base class in a generic case. The code is simplified from a library I am working on. So an explicit implementation for int and double is not an option. template struct Foo { virtual…
11
votes
3 answers

Zipping an `std::tuple` and variadic arguments

I have the following class: template class C { public: std::tuple... > maps; // Not real function: void foo(Tkeys... keys) { maps[keys] = 1; } }; How would I implement foo…
Baruch
  • 20,590
  • 28
  • 126
  • 201
11
votes
2 answers

Is it legal to use variadic templates in operator overloading?

I would like to be able to write something along these lines: struct bar {}; template bar operator+(bar, Args ...) {} I just checked with clang/gcc and the overloaded operator is picked up both by binary expressions (a+b) and…
11
votes
3 answers

What is wrong with this program calling a function pointer with a parameter pack?

According to my understanding, the following program should obviously print: 1.0 hello world 42 However, it fails to compile. Why? #include #include using namespace std; template void…
user253751
  • 57,427
  • 7
  • 48
  • 90
11
votes
1 answer

Variadic templates, parameter pack and its discussed ambiguity in a parameter list

In this question, I'll refer to my previous question. In that question, I found that the following is not valid: template class C { }; This is because: [It is not valid code] for class templates because their…
skypjack
  • 49,335
  • 19
  • 95
  • 187
11
votes
2 answers

Using sizeof... within std::enable_if

The following code does not compile, and I just can't figure out why. template typename std::enable_if 0>::type func() { // nop } The error message produced is: error: expected unqualified-id before numeric…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
11
votes
2 answers

Compile time array from C++ template parameter pack

How can I at compile time create for example an std::array from a template parameter pack? This shows what I need, but without a parameter pack. template struct ToInfoArray { static constexpr…
Mathias
  • 1,446
  • 2
  • 16
  • 31
11
votes
2 answers

C++11 "overloaded lambda" with variadic template and variable capture

I'm investigating a C++11 idiom which might be called "overloaded lambda": http://cpptruths.blogspot.com/2014/05/fun-with-lambdas-c14-style-part-2.html http://martinecker.com/martincodes/lambda-expression-overloading/ Overloading n functions with…
nodakai
  • 7,773
  • 3
  • 30
  • 60
11
votes
1 answer

Wrapping each type in a variadic template in a templated class

Given a variadic template Types..., I would like to store A<> for each of the types in the pack. This could be done in a tuple of A<>'s, but I'd need to programmatically derive the type of said tuple. Is such a thing even possible in c++11/14/17?…
Stijn Frishert
  • 1,543
  • 1
  • 11
  • 32