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
12
votes
1 answer

How to have a pointer to a function with arbitrary arguments as a template parameter?

This is a semantic-optimization problem I've been working on over the past couple days, and I'm stuck. My real program runs on a RTOS (FreeRTOS, specifically), and I need to spawn tasks (which are simplified, non-terminating versions of threads).…
Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
12
votes
1 answer

Pretty-print types and class template along with all its template arguments

Since typeid(T).name() doesn't return human understandable name of the type, it doesn't help us much if we want to print the name of the template arguments to some class template, especially when we're debugging. We often feel like writing this in…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
12
votes
1 answer

Eliminate duplicate entries from C++11 variadic template arguments

I'm using variadic templates with multiple virtual inheritance in C++ to aggregate types into a single structure definition. Here is a sample set of structures: struct meas { int i; }; struct meas2 : public virtual meas { int j; }; struct meas3 :…
12
votes
1 answer

Default parameter template vs variadic template : what is the last template parameter?

I'm a little confused because both a default parameter template and a variadic template parameter have to be the last parameter of a template. So what is the good official syntax for my function ? template
Vincent
  • 57,703
  • 61
  • 205
  • 388
11
votes
2 answers

Variadic Templates - different types of expansion

Andrei Alexandrescu gave an excellent talk entitled: Variadic Templates are Funadic. He presents the following 3 expansions which are subltey different: template void fun( Ts... vs ) { gun( A::hun(vs)...); gun(…
mark
  • 7,381
  • 5
  • 36
  • 61
11
votes
5 answers

random picker function using variadic templates -- is it possible?

I would like to use C++11's variadic templates to achieve a generalized "random picker" function. Something like this... template T randomPicker(T one, T two, T three) { int pick = 3 * (rand() / double(RAND_MAX)); switch (pick) …
s-v
  • 153
  • 1
  • 7
11
votes
2 answers

How to implement "Variadic Template" with pre-c++0x(VS2008)?

I'm using Visual Studio 2008, and I want to implement string formatting function without Variable Argument List. How to implement "Variadic Template" with pre-c++0x(VS2008)? Is there any library which implements this like boost? Or another way to…
winnerrrr
  • 669
  • 1
  • 7
  • 10
11
votes
1 answer

clang vs gcc: variadic lambda captures

I am trying to capture a variadic lambda argument inside a inner lambda and use it there. As an example, consider this code: int main () { auto first = [&] (auto&&... one) { auto second = [&] (auto&&... two) { return ((one *…
nnolte
  • 1,628
  • 11
  • 25
11
votes
3 answers

Understanding the declaration, definition and specialization of templates

I'm trying to understand the below example, but I'm a bit confused from the three different template and struct declarations. Could you please describe what will happen for the below call? which of the templates will be used and when? Also why does…
11
votes
2 answers

Create a type list combination of types in C++

Im trying to create some tool to create a list of types based on combinations of other types. Lets say we have three types struct A{}; struct B{}; struct C{}; I want to get a list of tuples which has every possible combination of N types A,B or…
11
votes
3 answers

Is it possible to insert extra operation in fold expression?

In C++17, fold expression is available, so to print arguments, we could use #define EOL '\n' template void output_argus(Args&&... args) { (cout << ... << args) << EOL; } int main() { output_argus(1, "test",…
r0n9
  • 2,505
  • 1
  • 29
  • 43
11
votes
3 answers

class constructor precedence with a variadic template constructor for a value wrapper

Today I've discovered that I don't understand the C++ constructor precedence rules. Please, see the following template struct wrapper template struct wrapper { T value; wrapper (T const & v0) : value{v0} { std::cout <<…
max66
  • 65,235
  • 10
  • 71
  • 111
11
votes
3 answers

Exposing parameter types in a perfectly-forwarding function avoiding code repetition

I have an annoying scenario where I need to defer the initialization of some object state and allow the user to construct one on demand. E.g. // user code context c; // ...do something... c.initialize_state(a, b, c); // library code class…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
11
votes
1 answer

Template template parameter and default values

Consider the following code: template struct A { }; // same as A, but with one extra defaulted parameter template struct B { }; template typename T> T build() { return {}; } int…
Floop
  • 451
  • 4
  • 10
11
votes
2 answers

Checking if variadic template parameters are unique using fold expressions

Given a variadic template parameter pack, I want to check if all types given to it are unique using an inline constexpr bool and fold expressions. I trie something like this: template inline static constexpr bool is_unique = (... &&…
Joald
  • 1,114
  • 10
  • 32