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

Apply function on each element in parameter pack

I have the following template function with specialization: // Pass the argument through ... template U convert(T&& t) { return std::forward(t); } // ... but convert std::strings const char* convert(std::string s) { …
Zitrax
  • 19,036
  • 20
  • 88
  • 110
13
votes
5 answers

How to handle unused warnings caused by empty template parameter pack expansions?

An issue I keep facing is one where the compiler complains about an unused variable, even though the variable is used, but it's only used inside a parameter pack expansion that happens to be empty for a specific instantiation. For example: template…
dcmm88
  • 1,445
  • 1
  • 12
  • 30
13
votes
4 answers

C++11 indexing template parameter packs at runtime in order to access Nth type

From this SO topic (and this blog post), I know how to access Nth type in a template parameter pack. For instance, one of the answers to the abovementioned SO question suggests this: template using NthTypeOf = typename…
narengi
  • 1,345
  • 3
  • 17
  • 38
13
votes
1 answer

C++ variadic template with doubles

The following code #include #include template const std::vector*make_from_ints(int args...) { return new std::vector(std::initializer_list{args}); } is compiling (with GCC 6.3, on…
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
13
votes
4 answers

How to call native es6 template string replacement from tag function?

I'm writing a es6 tag function for template literals, which first checks a condition in the string and, if the condition isn't found, merely interprets the template literal as if it were untagged. I am curious if, from my tag function, there is a…
Aaron_H
  • 1,623
  • 1
  • 12
  • 26
13
votes
3 answers

Is it legal to partially specialise variadic template inner class with args from variadic template of an outer class

Consider the code: #include template struct outer { template struct inner { static constexpr bool value = false; }; template struct inner { static…
W.F.
  • 13,888
  • 2
  • 34
  • 81
13
votes
2 answers

Variadic templates and functions' pointers: what compiler is right?

I've not been able to find a better title, but feel free to modify it if you have the right idea. As it is, it's better than GCC vs clang anyway. I'm trying to figure out what's wrong in this code: template struct S; template…
skypjack
  • 49,335
  • 19
  • 95
  • 187
13
votes
2 answers

A variadic template method to accept a given number of doubles?

template class myclass { public: template void mymethod(Args... args) { // Do interesting stuff } }; I want mymethod to be called only with exactly N doubles. Is that possible? That is, say…
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
13
votes
1 answer

Calling virtual method of base template from derived variadic template class

This is essentially a follow-up to an earlier question (not posed by me, but I am interested in an answer). The question is: Why does the compiler/linker fail to resolve the call to the virtual function from the derived class? In this case, the…
jxh
  • 69,070
  • 8
  • 110
  • 193
13
votes
3 answers

How can a type be removed from a template parameter pack?

I'm searching for a way to remove (let's say for now all occurences of) a type from a template parameter pack. The end result would be a struct that looked like this : template struct RemoveT { using type = /* a new…
13
votes
2 answers

How to create a variadic template function with `std::function` as a function parameter?

How can I create a variadic template function with std::function as a function parameter that accepts a variadic number of arguments? I tried to reduce the problem to a MWE: #include template void run(std::function
13
votes
2 answers

"Template argument for template template parameter must be a class template or type alias template"

template struct List { }; template class> struct ListHelper; template struct ListHelper> { }; ^ …
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
13
votes
2 answers

Variadic template aliases as template arguments (part 2)

This is a follow-up of another question. It refers to the same problem (I hope) but uses an entirely different example to illustrate it. The reason is that in the previous example only experimental GCC 4.9 failed with a compiler error. In this…
13
votes
3 answers

Variadic Function Accepting Functors/Callable Objects

Problem I wish to make a function which accepts an arbitrary number of functor objects or more generally just callable objects (of different types) and applies them to an internal data structure. The function will be used with differing numbers of…
Dan
  • 12,857
  • 7
  • 40
  • 57
13
votes
1 answer

Variadic template function: specialize head/tail and empty base case

I'd like to have a variadic template function inside a class. The variadic template arguments are chars which should be processed in a loop-like manner. So I thought of writing it like in haskell with head/tail splitting the list until a base case…
leemes
  • 44,967
  • 21
  • 135
  • 183