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

Swap two parameters in variadic template

I'm trying to swap two items of a parameter pack. Ideally, I would like to do something like this: template void swapped_copy(some_class a, some_class b, Args... args) { a(args...) = b(/* 'args...' where parameters…
Limmershin
  • 135
  • 4
12
votes
2 answers

Using fold expressions to print all variadic arguments with newlines inbetween

The classic example for C++17 fold expressions is printing all arguments: template void print(Args ... args) { (cout << ... << args); } Example: print("Hello", 12, 234.3, complex{12.3f,…
Thomas McGuire
  • 5,308
  • 26
  • 45
12
votes
1 answer

Does C++ allow normal parameters after variadic template parameters?

According to cppreference, the following code is legal: lock_guard( MutexTypes&... m, std::adopt_lock_t t ); However, the following code cannot be compiled with clang 3.8 (-std=c++1z): template void f(Args&&..., bool) {} int…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
12
votes
3 answers

Ambiguous call when recursively calling variadic template function overload

Consider this piece of code: template void foo() { } template void foo() { foo(); } int main() { foo(); return 0; } It does not compile due to…
rubix_addict
  • 1,811
  • 13
  • 27
12
votes
1 answer

How to check if every type in a parameter pack is unique?

For a fixed number of template parameters it is easy, although the number of manually written checks grows quadratically. #include template < typename T1, typename T2, typename T3, typename T4> struct unique_types { …
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
12
votes
2 answers

Why clang rejects variadic template friend function

I have the following sample code, reduced to the essential, that compiles with gcc 6.1, gcc 7.0 head and Visual Studio 2015/2017RC, but not with any clang version. #include #include using namespace std; namespace outer { …
Felix Petriconi
  • 675
  • 5
  • 11
12
votes
2 answers

Function template parameter pack not at the end of the parameter list

The following code compiles and runs ok. void foo() { } template void foo(T x, Args... args) { cout << x << endl; foo(args...); } // inside main() foo(1,1,1); This other code does not compile: void foo()…
12
votes
3 answers

Multiple Variadic Parameter Pack for Template Class

I am using variadic parameter packs for policy based class design. template class IShader : public Policies... { }; Policies are defined when called or with defaults if none are specified. The problem comes…
James
  • 173
  • 3
  • 12
12
votes
1 answer

C++ : Check if the template type is one of the variadic template types

Let's say we have function: template void foo(){...}; What is the simplest way to check if the type 'Kind' is one of the types 'Kinds' in C++ (including C++1z)?
Michał
  • 655
  • 6
  • 19
12
votes
1 answer

Forwarding arguments to template member function

ideone example I need to forward some predefined arguments plus some user-passed arguments to a member function. #define FWD(xs) ::std::forward(xs) template void forwarder(void(T::*fptr)(Ts...),…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
12
votes
2 answers

Equivalent of std::transform for tuples

I want a function that will behave like std::transform for tuples. Basically the functionality to be implemented is template void tuple_transform(Tuple&& source, Tuple&& target, Func f) { //…
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
12
votes
1 answer

Expanding a parameter pack of templateclasses

Suppose I have some template classes defined as follows templateclass...> struct my_class; template struct define_template{ template class type; }; I need to define an alias template that substitutes…
Tim
  • 693
  • 3
  • 11
12
votes
1 answer

Mixing variadic template values and variadic deduced types

Is the following perfectly defined by the standard ? #include template void f(Types&&... values) { std::cout<
Vincent
  • 57,703
  • 61
  • 205
  • 388
12
votes
3 answers

Casting a variadic parameter pack to (void)

I've effectively got the following problem: I want to be able to build with -Wall -Wextra -Werror, however, the following code will complain about unused parameters: struct foo { template static void bar() { } …
Yuushi
  • 25,132
  • 7
  • 63
  • 81
12
votes
1 answer

How to use std::enable_if to conditionally select a variadic constructor?

I am trying to make a class which should inherit the constructors from other classes but without inheriting from those classes itself. At one point during the initialization of my class, I want to use perfect forwarding to create an object of the…
user1492625
  • 313
  • 5
  • 10