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
60
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
  • 53,344
  • 14
  • 119
  • 168
60
votes
3 answers

How to call a function on all variadic template args?

I would like to do template void print(ArgTypes... Args) { print(Args)...; } And have it be equivalent to this quite bulky recursive chain: template void print(const T& t, ArgTypes...…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
58
votes
5 answers

template parameter packs access Nth type and Nth element

The following paper is the first proposal I found for template parameter packs. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1603.pdf At page 16, it talks about introducing two new operators [] and <> for accessing parameter pack…
nurettin
  • 11,090
  • 5
  • 65
  • 85
57
votes
3 answers

c++ lambdas how to capture variadic parameter pack from the upper scope

I study the generic lambdas, and slightly modified the example, so my lambda should capture the upper lambda's variadic parameter pack. So basically what is given to upper lambda as (auto&&...) - should be somehow captured in [=] block. (The perfect…
barney
  • 2,172
  • 1
  • 16
  • 25
53
votes
6 answers

Restrict variadic template arguments

Can we restrict variadic template arguments to a certain type? I.e., achieve something like this (not real C++ of course): struct X {}; auto foo(X... args) Here my intention is to have a function which accepts a variable number of X…
bolov
  • 72,283
  • 15
  • 145
  • 224
50
votes
6 answers

recursive variadic template to print out the contents of a parameter pack

How is it possible to create a recursive variadic template to print out the contents of a paramater pack? I am trying with this, but it fails to compile: template std::string type_name () { return…
Gabor Marton
  • 2,039
  • 2
  • 22
  • 33
50
votes
2 answers

Pack expansion for alias template

It seems that a pack argument can be expanded only in the place of a pack parameter of an alias template. This is not true for a class or a function template: template struct x { using type = T; }; template
bolov
  • 72,283
  • 15
  • 145
  • 224
46
votes
1 answer

How do I get the argument types of a function pointer in a variadic template class?

This is a follow up of this problem: Generic functor for functions with any argument list I have this functor class (full code see link above): template class Foo { std::function m_f; public: …
steffen
  • 8,572
  • 11
  • 52
  • 90
46
votes
2 answers

Is it possible to write a function template which returns whether the number of arguments is divisible by N?

I've been learning about variadic templates, and with the help of this excellent blog post, I've managed to write a function template even_number_of_args which returns whether the number of arguments it receives is divisible by 2. #include…
mwhittaker
  • 1,745
  • 15
  • 18
45
votes
1 answer

Partial specialization of variadic templates

Consider the following class template 'X' and its partial specializations. template struct X {}; // #1 template struct X {}; // #2 template struct X
45
votes
3 answers

C++ variadic template template argument that matches any kind of parameters

I was wondering if it's possible to write a template function that can take any other arbitrary template as a parameter and properly match the template name (i.e. not just the resulting class). What I know to work is this: template
Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
44
votes
5 answers

Generating one class member per variadic template argument

I have a template class where each template argument stands for one type of value the internal computation can handle. Templates (instead of function overloading) are needed because the values are passed as boost::any and their types are not clear…
user1101674
  • 1,341
  • 2
  • 12
  • 15
44
votes
6 answers

What is the easiest way to print a variadic parameter pack using std::ostream?

What is the easiest way to print a parameter pack, separated by commas, using std::ostream? Example: template void doPrint(std::ostream& out, Args... args){ out << args...; // WRONG! What to write here? } // Usage: int main(){ …
gexicide
  • 38,535
  • 21
  • 92
  • 152
42
votes
4 answers

How can I check type T is among parameter pack Ts...?

I want to write a function to return true if T is one of Ts... template bool is_one_of(); For example, is_one_of returns true, and is_one_of returns…
Yixing Liu
  • 2,179
  • 1
  • 20
  • 36
42
votes
2 answers

Why is this nested variadic template an invalid argument?

If I define a struct template Bar which accepts a template argument: template