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
288
votes
10 answers

"unpacking" a tuple to call a matching function pointer

I'm trying to store in a std::tuple a varying number of values, which will later be used as arguments for a call to a function pointer which matches the stored types. I've created a simplified example showing the problem I'm struggling to…
Flexo
  • 87,323
  • 22
  • 191
  • 272
226
votes
6 answers

make_unique and perfect forwarding

Why is there no std::make_unique function template in the standard C++11 library? I find std::unique_ptr p(new SomeUserDefinedType(1, 2, 3)); a bit verbose. Wouldn't the following be much nicer? auto p =…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
145
votes
1 answer

How would one call std::forward on all arguments in a variadic function?

I was just writing a generic object factory and using the boost preprocessor meta-library to make a variadic template (using 2010 and it doesn't support them). My function uses rval references and std::forward to do perfect forwarding and it got me…
118
votes
2 answers

What is the meaning of "... ..." token? i.e. double ellipsis operator on parameter pack

While browsing through gcc's current implementation of new C++11 headers, I stumbled upon "......" token. You can check, that the following code compiles fine [via godbolt.org]. template struct X { /* ... */ }; template
Vitus
  • 11,822
  • 7
  • 37
  • 64
105
votes
2 answers

What are the rules for the "..." token in the context of variadic templates?

In C++11 there are variadic templates like this one: template< class T, class... Args > unique_ptr make_unique( Args&&... args ) { return unique_ptr(new T(std::forward(args)...)); } There are some curiosities about this: The…
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
104
votes
2 answers

C++11: Number of Variadic Template Function Parameters?

How can I get a count of the number of arguments to a variadic template function? ie: template void f(const T&... t) { int n = number_of_args(t); ... } What is the best way to implement number_of_args in the above?
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
100
votes
13 answers

Pretty-print std::tuple

This is a follow-up to my previous question on pretty-printing STL containers, for which we managed to develop a very elegant and fully general solution. In this next step, I would like to include pretty-printing for std::tuple, using…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
98
votes
4 answers

How to store variadic template arguments?

Is it possible to store a parameter pack somehow for a later use? template class Action { private: std::function f; T... args; // <--- something like this public: Action(std::function f,…
Eric B
  • 4,367
  • 5
  • 33
  • 43
94
votes
6 answers

Variadic template pack expansion

I am trying to learn variadic templates and functions. I can't understand why this code doesn't compile: template static void bar(T t) {} template static void foo2(Args... args) { (bar(args)...); } int main() { …
Viacheslav Dronov
  • 1,029
  • 1
  • 8
  • 11
82
votes
9 answers

How can I iterate over a packed variadic template argument list?

I'm trying to find a method to iterate over an a pack variadic template argument list. Now as with all iterations, you need some sort of method of knowing how many arguments are in the packed list, and more importantly how to individually get data…
graphitemaster
  • 3,483
  • 4
  • 19
  • 14
80
votes
5 answers

How can I have multiple parameter packs in a variadic template?

Function one() accepts one parameter pack. Function two() accepts two. Each pack is constrained to be wrapped in types A and B. Why is it impossible to instantiate two()? template struct A {}; template struct B…
Samuel Danielson
  • 5,231
  • 3
  • 35
  • 37
77
votes
5 answers

How to make generic computations over heterogeneous argument packs of a variadic template function?

PREMISE: After playing around with variadic templates a little bit, I realized that achieving anything which goes slightly beyond the trivial meta-programming tasks soon becomes pretty cumbersome. In particular, I found myself wishing for a way to…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
76
votes
4 answers

Is it possible to "store" a template parameter pack without expanding it?

I was experimenting with C++0x variadic templates when I stumbled upon this issue: template < typename ...Args > struct identities { typedef Args type; //compile error: "parameter packs not expanded with '...' }; //The following code just shows…
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
66
votes
7 answers

How to use source_location in a variadic template function?

The C++20 feature std::source_location is used to capture information about the context in which a function is called. When I try to use it with a variadic template function, I encountered a problem: I can't see a place to put the source_location…
L. F.
  • 19,445
  • 8
  • 48
  • 82
65
votes
3 answers

Is there a name for this tuple-creation idiom?

On the Boost mailinglist, the following clever trick to create a tuple-like entity was recently posted by @LouisDionne: #include auto list = [](auto ...xs) { return [=](auto access) { return access(xs...); }; }; auto length =…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
1
2 3
99 100