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
15
votes
7 answers

C++ function call wrapper with function as template argument

I'm trying to create a generic wrapper function that takes a function as a template argument and takes the same arguments as that function as its arguments. For example: template /* return type of F */ wrapper(Ts... Args /* not…
Eric
  • 165
  • 1
  • 1
  • 6
15
votes
1 answer

Are variadic arguments after a defaulted parameter well-formed?

template void bark( int = 0, Args&&... args ) {} int main() { bark(); bark(1); bark(1, 2); } Is this code well-formed according to the C++ Standard? The proposed duplicate does not contain the same calls of the…
Puppy
  • 144,682
  • 38
  • 256
  • 465
15
votes
1 answer

How can I expand call to variadic template base classes?

I have a set of non-orthogonal policies, all of them implementing a common named method, the policies add safety checks. I want users to be able to combine the policies to allow more complex validation without creating policies for each combination…
dvicino
  • 1,469
  • 1
  • 11
  • 19
15
votes
5 answers

Checking type of parameter pack using enable_if

Since there is a restriction on allowed non-type variadic templates, I am trying to write a function taking an arbitrary number of doubles using enable_if. In essence, I want to do something like: template
user1997744
  • 411
  • 4
  • 16
15
votes
3 answers

Wrap a function pointer in C++ with variadic template

The Question I have a number of C++ functions void f(), R g(T a), S h(U a, V b) and so on. I want to write a template function that accepts f, g, h and so on as a template argument and calls that function. ie I want something like…
0xbe5077ed
  • 4,565
  • 6
  • 35
  • 77
15
votes
1 answer

Unpacking parameter packs in template aliases

I run into a problem with unpacking variadic templates into a template alias. The following code works with Clang 3.4 and GCC 4.8 but fails with GCC 4.9: template using front_type = T; template struct…
mavam
  • 12,242
  • 10
  • 53
  • 87
15
votes
1 answer

isn't non-type parameter pack that evaluates to "void..." illegal?

gcc-4.8 accepts this code, but isn't it wrong since the non-type parameter pack is equivalent to void... which is illegal? template ::value>::type...> void test(T) {} I tried this…
Hui
  • 337
  • 2
  • 5
15
votes
1 answer

x := [...]string{"Sat", "Sun"} vs x:= []string{"Sat", "Sun"}

In the go lang spec they used three dots in one of the examples: days := [...]string{"Sat", "Sun"} // len(days) == 2 Does it make any difference if the three dots are left out?
Rusty Rob
  • 16,489
  • 8
  • 100
  • 116
15
votes
3 answers

C++11 variadic templates: return tuple from variable list of vectors

I want to write something similar to python zip (http://docs.python.org/2/library/functions.html). zip should take in a variable number of vectors of different types and it returns a vector of tuples, truncated to the length of the shortest…
duli
  • 1,621
  • 3
  • 16
  • 25
15
votes
2 answers

Code I've never seen in C++11

I'm looking at this source code template struct conv2bin; template struct conv2bin { static_assert(high == '0' || high == '1', "no bin num!"); static int const value = (high - '0')…
15
votes
1 answer

Can X x(t...) ever result in a function declaration with vexing parse?

I was writing a function template as template void f(T ...t) { X x(t...); // ... } When I was looking at it, I was wondering what happens for a call f(). Will vexing parse make x a function declaration? Compilers seem to make it…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
14
votes
2 answers

How to pass a reference through a parameter pack?

I have the following code: #include template void foo(Fun f, Args... args) { f(args...); } int main() { int a = 2; int b = 1000; foo([](int &b, int a){ b = a; }, b, a); std::printf("%d\n",…
p12
  • 1,161
  • 8
  • 23
14
votes
2 answers

Template template parameters and variadic templates with gcc 4.4

I'm using gcc 4.4 on Debian squeeze. Consider the following code. #include #include using std::map; using std::string; // Args lets the user specify additional explicit template arguments template
Faheem Mitha
  • 6,096
  • 7
  • 48
  • 83
14
votes
5 answers

How to make grouped or paired fold of parameter pack?

template std::wstring descf(Msg, Args&&... args) { std::wostringstream woss; owss << Msg << ". " << ... << " " << args << ": '" << args << "' ";//not legal at all //or owss << Msg << ". " << args[0] << ":…
darune
  • 10,480
  • 2
  • 24
  • 62
14
votes
2 answers

Transform variadic template parameters to another types

How to transform types from variadic template parameters to another type? For example: template struct single { std::tuple m_single; }; template struct sequences { single get(size_t pos) { …
Max
  • 1,037
  • 8
  • 9