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

Expanding parameter pack as part of lambda capture in fold expression - gcc vs clang

Consider the following code snippet: template void foo() { ([i = Is]{}(), ...); } clang++ (trunk) successfully compiles the code with -std=c++17 g++ (trunk) fails to compile with the following error: : In function 'void…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
14
votes
2 answers

Is it possible to get the first type of a parameter pack in a one-liner?

I have a parameter pack given in a variadic class template and want to extract the first type. Currently I do this, which works fine but is somehow cumbersome. Is it possible to do the same thing simpler? FirstEntityType should be defined to have…
dani
  • 3,677
  • 4
  • 26
  • 60
14
votes
2 answers

Mixing types and nontypes in variadic template parameters?

Is it possible to do mixing of types and nontypes in variadic template parameters? If I were to pass a std::array for instance to this class as parameter T, I would need to also pass a type for the array and a length, but the way I tried it below…
lo tolmencre
  • 3,804
  • 3
  • 30
  • 60
14
votes
4 answers

Implicit conversions with variadic templates

Consider two function calls foo({"a", 1}, {"b", "value"}); foo({"a", 1}, {"b", "value"}, {"c", 1.0}); Is there a way to write function foo for arbitrary number of argument pairs? I was thinking something along the lines template
14
votes
1 answer

Template arguments deduction for parameter type of function pointer involving non-deduced parameter pack

This is similar to the question, but a more specific case. This time, no compiler work as expected. template struct nondeduced { using type = T; }; template using nondeduced_t = typename nondeduced::type; template
14
votes
3 answers

Event emitter and automatic registrations of member methods as listeners

This is a question with answer, the aim of which is to invite the readers to suggest their own solutions. I'm quite sure that out there there are more clever approaches than mine, so I'd like to know what those solutions are. Please, share your…
skypjack
  • 49,335
  • 19
  • 95
  • 187
14
votes
1 answer

Testing if member function exists using variadics

So I'm very familiar with the paradigm of testing if a member function exists. Currently this code works: #include #include struct has_mem_func_foo_impl { template struct chk { }; template…
cdacamara
  • 223
  • 1
  • 7
14
votes
2 answers

Check if valid template specialization

I want to check whether or not a template can be specialized using a given set of arguments. Here is the version for templates accepting only 1 argument: #include template class C, typename T> struct…
Tom Knapen
  • 2,277
  • 16
  • 31
14
votes
1 answer

How to combine std::bind(), variadic templates, and perfect forwarding?

I want to invoke a method from another, through a third-party function; but both use variadic templates. For example: void third_party(int n, std::function f) { f(n); } struct foo { template void invoke(int n,…
piwi
  • 5,136
  • 2
  • 24
  • 48
14
votes
2 answers

Is it possible to have a non recursive at_c implementation?

Long time back I had seen a non-recursive implementation to get the last value/type from a type sequence/value sequence. It has a nice property, that the number of template instantiated is independent (and constant) of the number of elements the…
abir
  • 1,797
  • 14
  • 26
14
votes
2 answers

How can C++ and C variadic arguments be used together?

Generally, using the C++11 variadic template feature with functions requires the variadic-based function arguments to be the last in the function argument list. There is one exception; they are the next-to-last arguments if there are C-level…
CTMacUser
  • 1,996
  • 1
  • 16
  • 27
14
votes
2 answers

(Im)perfect forwarding with variadic templates

Synopsis Given a type with a variadic template constructor that forwards the arguments to an implementation class, is it possible to restrict the types being forwarded with SFINAE? Details First, consider the non-variadic case with a constructor…
mavam
  • 12,242
  • 10
  • 53
  • 87
13
votes
1 answer

Clang fails to expand parameter pack in std::function instantiation

The snippet of code compiled with std=c++17 as the only compiler flag ... ... compiles successfully with GCC 9.1. Godbolt ... issues a compiler error with Clang 8.0.0 (error below snippet). Godbolt Question: is this a bug in the Clang compiler or…
Maarten Bamelis
  • 2,243
  • 19
  • 32
13
votes
1 answer

Non Deduced context for a non type parameter

I am reading C++ Templates (2nd edition) and this is a snippet from the book: template void f(double (&)[N+1], Ts... ps) {return;} It is specified in the book that the declaration above is useless because N cannot be…
MSS
  • 553
  • 2
  • 11
13
votes
2 answers

How to pass std::function with different parameters to same function

I have three functions I'm looking to merge together. Each one takes an std::function as the first parameter, then executes it within a try/catch block. The issue is, there are three different types of functions. Functions with no parameters, those…
Griffort
  • 1,174
  • 1
  • 10
  • 26