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
53
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
  • 69,525
  • 30
  • 173
  • 306
52
votes
5 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.
  • 16,219
  • 7
  • 33
  • 67
47
votes
5 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
  • 58,757
  • 13
  • 108
  • 182
47
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
  • 9,834
  • 5
  • 53
  • 77
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,523
  • 13
  • 18
44
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
44
votes
1 answer

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
  • 58,757
  • 13
  • 108
  • 182
42
votes
5 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
  • 1,829
  • 2
  • 21
  • 31
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