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
3
votes
2 answers

Make variadic macro / method which prints all variables names and values

An existing macro gets variadic number of variables on my application. I want, using this macro, to print these variables by name=value format. Here is a small example : #define EXISTING_MACRO(...) < ??? > int main() int a = 0; int b = 1; int c =…
SagiLow
  • 5,721
  • 9
  • 60
  • 115
3
votes
2 answers

C++11 variadic template + inheritance

I would like to write an abstraction of linear superpositions using variadic templates. To do that, I would like to define a base type that exhibits a certain form of operator() like so template class…
sblatt
  • 33
  • 1
  • 3
3
votes
0 answers

Variadic alias template to non-variadic class template

In trying to write a simple example for currying of metafunction classes, I wrote the following: #include struct first { template using apply = T; }; template struct…
Barry
  • 286,269
  • 29
  • 621
  • 977
3
votes
1 answer

Why does the variadic template argument deduction fail for this function pointer?

In the following minimal example, S::foo works, but S::bar fails. The only difference is the order of the parameter packs Ts and Us. struct FPtr and S::lol are the best workaround I've found, but it's rather uncomfortable to use in practice. Why…
mic_e
  • 5,594
  • 4
  • 34
  • 49
3
votes
2 answers

Variadic Templates and RValue refs

Consider the following C++ code template void f (const int x, const int y, Args&&... args) { // Do something } As far as I understand,Args here could either be lvalue or rvalue references, depending on the type deduction at…
ssb
  • 7,422
  • 10
  • 36
  • 61
3
votes
3 answers

C++ template sort

I'm looking for template code to sort template parameters, something like: template list sort(T first, Args ... rest) All the types in Args are actually T, but I need to use variadic templates, so that I can small…
Frank
  • 4,341
  • 8
  • 41
  • 57
3
votes
2 answers

A structure that stores its fields by size

I would like to know how can I do the following in C++: Consider these classes : C1 < C2 < C3 < ... < Cn, Ci < Cj means sizeof(Ci) < sizeof(Cj) I want a structure that uses variadic templates as a sequence of Ci's, OrderBySize, for…
Othman Benchekroun
  • 1,998
  • 2
  • 17
  • 36
3
votes
3 answers

Variadic template unrolling to std::tuple

I have a filter class that takes two template parameters, the number of inputs and the number of outputs. template class Filter { // implementation }; Sometimes I need to chain multiple filters in series so I was thinking to…
3
votes
3 answers

Variadic template method and std::function - compilation error

I'm sure the error is very simple and silly, but I can't see one. Here's the code: #include template class Foo { public: template void exec(const std::function& task,…
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
3
votes
4 answers

Variadic Template Parameter Packs with Alternating Types

I was wondering if it is possible to capture an alternating parameter pattern using a parameter pack. For example, template class foo { public: foo() : my_T(nullptr), my_U(U) {} …
armstrhu
  • 303
  • 3
  • 8
3
votes
1 answer

Variadic template function name lookup fails to find specializations

I am attempting to program a string concatenation function which utilizes my 3D library's string conversion functions, implemented with a variadic template. The library's conversion function behaves unusually if a string (either const char[] literal…
user3995702
3
votes
1 answer

Expanding container to parameter pack

Let's say I have a variadic function I can't change like so: void Func(int count, ...) { /*etc.*/ } And I'm writing a variadic template like so to wrap that function and make it safer to use: template void ToString(T... args) { …
HTAPAWASO
  • 70
  • 3
3
votes
1 answer

Weird overload resolution with variadic function templates

I have the following code: #include template void f(int a, int b, Args... args) { std::cout << b << '\n'; f(a, args...); } void f(int, int b) { std::cout << b << '\n'; } int main() { f(1, 2); …
Lingxi
  • 14,579
  • 2
  • 37
  • 93
3
votes
1 answer

C++: Using type as map key introduces template substitution errors

I'm using a tuple as a key to track elements in a map, and then later iterating over the map to produce a string version of the map. To help me with the conversion, I have a template convenience function that will concatenate the tuple used as the…
sabreitweiser
  • 643
  • 3
  • 13
3
votes
2 answers

Variadic Template simulating "runtime" expansion

I have a function that I am trying to convert it to use variadic templates. Unfortunately, the template expansion causes problems when attempting to strongly type the functions during compile time. Here is the old…
user3072517
  • 513
  • 1
  • 7
  • 21