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

3375 questions
264
votes
8 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
  • 82,006
  • 22
  • 174
  • 256
220
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
  • 237,063
  • 85
  • 359
  • 638
122
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…
110
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 ideone.com]. template struct X { /* ... */ }; template
Vitus
  • 11,413
  • 7
  • 32
  • 62
100
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
  • 20,730
  • 9
  • 61
  • 117
93
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
  • 58,923
  • 32
  • 156
  • 267
92
votes
12 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
  • 428,875
  • 83
  • 813
  • 1,025
90
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,077
  • 5
  • 29
  • 39
80
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
  • 889
  • 1
  • 7
  • 11
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
  • 114,596
  • 21
  • 355
  • 432
73
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
  • 72,907
  • 15
  • 82
  • 134
64
votes
4 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
  • 4,351
  • 2
  • 30
  • 31
63
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
  • 65,583
  • 16
  • 147
  • 283
59
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,055
  • 4
  • 17
  • 14
54
votes
3 answers

Variadic template templates and perfect forwarding

This question on the object generator pattern got me thinking about ways to automate it. Essentially, I want to automate the creation of functions like std::make_pair, std::bind1st and std::mem_fun so that instead of having to write a different…
Peter Alexander
  • 50,304
  • 10
  • 114
  • 163
1
2 3
99 100