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

C2977: 'std::tuple' : too many template arguments (MSVC11)

I'm trying to build googletest with Visual C++ 11, but following code causes an error template void PrintTo(const…
Loom
  • 9,768
  • 22
  • 60
  • 112
41
votes
6 answers

How to make a variadic is_same?

How can I make a class template that returns whether any of its variadic types are equal to the first type. I want to be able to do this: is_same::value; // true if T is one of A, B or C And if T is equal to any one of those types, its…
Me myself and I
  • 3,990
  • 1
  • 23
  • 47
40
votes
2 answers

Expansion with variadic templates

What is the difference between the following 3 calls for gun function? template void fun(Ts... vs) { gun(A::hun(vs)...); gun(A::hun(vs...)); gun(A::hun(vs)...); } I am interested in an answer that explains the…
Laura Maftei
  • 1,863
  • 1
  • 15
  • 25
40
votes
1 answer

Understanding the overhead of lambda functions in C++11

This was already touched in Why C++ lambda is slower than ordinary function when called multiple times? and C++0x Lambda overhead But I think my example is a bit different from the discussion in the former and contradicts the result in the…
mcbulba
  • 523
  • 1
  • 4
  • 7
40
votes
6 answers

How to reverse the order of arguments of a variadic template function?

I have a template function with varargs template arguments, like this template void ascendingPrint(Args... args) { /* ... */ } And I want to write template void descendingPrint(Args... args) { /* implementation…
towi
  • 21,587
  • 28
  • 106
  • 187
39
votes
6 answers

Simulate variadic templates in C#

Is there a well-known way for simulating the variadic template feature in C#? For instance, I'd like to write a method that takes a lambda with an arbitrary set of parameters. Here is in pseudo code what I'd like to have: void…
nakhli
  • 4,009
  • 5
  • 38
  • 61
38
votes
2 answers

generic member function pointer as a template parameter

Consider this code: #include using namespace std; class hello{ public: void f(){ cout<<"f"<
Lorenzo Pistone
  • 5,028
  • 3
  • 34
  • 65
38
votes
2 answers

Confusing templates in C++17 example of std::visit

When looking at std::visit() page in cppreference, https://en.cppreference.com/w/cpp/utility/variant/visit, I encountered the code I can't make sense of... Here's the abbreviated version: #include #include #include…
Boris
  • 748
  • 6
  • 18
38
votes
4 answers

Generating a sequence of zeros at compile time

I have the following problem: template< size_t... N_i > class A { public: // ... void foo() { bar( /* 0,...,0 <- sizeof...(N_i) many */); } }; I want to call a function bar and pass sizeof...(N_i) many arguments to it…
abraham_hilbert
  • 2,221
  • 1
  • 13
  • 30
38
votes
10 answers

Implementing variadic min / max functions

I'm implementing variadic min/max functions. A goal is to take advantage of the compile time known number of arguments and perform an unrolled evaluation (avoid run-time loops). The current state of the code is as follows (presenting min - max is…
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
37
votes
1 answer

Is there a reason to use std::conjunction/std::disjunction instead of a fold expression over "&&"/"||"?

Is there any specific cases you cannot correctly do with std::conjunction/std::disjunction and not using the more "fundamental" (i.e. language feature instead of library feature) fold expression over &&/||? Example: // func is enabled if all Ts...…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
37
votes
3 answers

details of std::make_index_sequence and std::index_sequence

I'm enjoying ramping up on variadic templates and have started fiddling about with this new feature. I'm trying to get my head around the implementation details of std::index_sequence's (used for tuple implementation). I see sample code around…
36
votes
2 answers

Get the Nth type of variadic template templates?

How to get the Nth type of variadic template templates? For example template class MyClass { Args[0] mA; // This is wrong. How to get the type? };
user1899020
  • 13,167
  • 21
  • 79
  • 154
35
votes
4 answers

How can I prevent a variadic constructor from being preferred to the copy constructor?

I have a template 'Foo', which owns a T, and I'd like it to have a variadic constructor that forwards its arguments to T's constructor: template struct Foo { Foo() : t() {} Foo(const Foo& other) : t(other.t)…
acm
  • 12,183
  • 5
  • 39
  • 68
35
votes
2 answers

C++11: I can go from multiple args to tuple, but can I go from tuple to multiple args?

Possible Duplicate: How do I expand a tuple into variadic template function's arguments? “unpacking” a tuple to call a matching function pointer In C++11 templates, is there a way to use a tuple as the individual args of a (possibly template)…
Thomas
  • 4,208
  • 2
  • 29
  • 31