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

Expression contains unexpanded parameter packs

Somehow I don't get how variadic template parameter packs are expanded. What's wrong with thie following code? #include template struct print_one { static void run(const T& t) { std::cout << t << ' '; …
marton78
  • 3,899
  • 2
  • 27
  • 38
16
votes
2 answers

Specializing a variadic template template parameter on the minimum number of arguments: legal or not?

I have code: #include template class> struct Foo { enum { n = 77 }; }; template class C> struct Foo { enum { n = 99 }; }; template struct A {…
glaebhoerl
  • 7,695
  • 3
  • 30
  • 41
16
votes
2 answers

Implementing meta-function zip in c++11

I am actually trying to see if I can get a minimal library that supports the very few operations I use from boost::fusion. Here is what I have so far... template < typename... Types > struct typelist { }; template < template < typename... > class…
zrb
  • 721
  • 1
  • 6
  • 15
16
votes
3 answers

Variadic templates: unfold arguments in groups

I have a function that takes two arguments: template void foo(T1 arg1, T2 arg2) { std::cout << arg1 << " + " << arg2 << '\n'; } And a variadic one that should forward its arguments in pairs : template
Fourmet
  • 538
  • 2
  • 12
16
votes
4 answers

How to retrieve variadic template parameters without auxillary function template?

Suppose I have template struct Ints { }; class MyClass { public: Ints<1, 2, 3> get() { return Ints<1, 2, 3>(); } }; What I want to do is simple. template vector MyFunc1(T& x) { Ints result = x.get(); …
i.stav
  • 444
  • 3
  • 9
16
votes
2 answers

c++ generic compile-time for loop

In some contexts, it could be useful/necessary to have a for loop evaluated/unrolled at compile time. For example, to iterate over the elements of a tuple, one needs to use std::get, which depends on a template int parameter I, hence it has to be…
francesco
  • 7,189
  • 7
  • 22
  • 49
16
votes
2 answers

Fold expression with comma operator and variadic template parameter pack

#include using namespace std; template void output_argus(Args&&... args) { ((cout << args << '\n'), ...); // #1 (... , (cout << args << '\n')); // #2 } int main() { output_argus(1, "test",…
Ke Zhang
  • 937
  • 1
  • 10
  • 24
16
votes
9 answers

Typesafe variadic function

I want to write a function that accepts a variable number of string literals. If I was writing in C, I would have to write something like: void foo(const char *first, ...); and then the call would look like: foo( "hello", "world", (const…
16
votes
1 answer

Partial template specialization with multiple template parameter packs

Continuing my journey into the world of variadic templates, I encountered another problem. Assuming the following template class: template < typename T > struct foo { //default implementation }; it is possible to partially specialize it for…
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
16
votes
2 answers

What exactly is a "trailing parameter pack"

In resolving ambiguities between function template overloads, partial ordering is performed (see here for some explanations). In that website, we also learn that In case of a tie, if one function template has a trailing parameter pack and the…
Walter
  • 44,150
  • 20
  • 113
  • 196
16
votes
2 answers

Limit the number of parameters in a variadic template parameter pack

I have a template function that takes a variable number of arguments. Since you can't force the arguments to be of a certain type I would like at least to force the number of arguments not to be higher that a compile-time determined number(e.g.…
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
16
votes
2 answers

SFINAE not happening with std::underlying_type

Below SFINAE code with variadic templates compiles nicely using clang 3.7.1, C++14: #include #include #include #include enum class Bar : uint8_t { ay, bee, see }; struct S { static void foo() {} //…
Paolo M
  • 12,403
  • 6
  • 52
  • 73
16
votes
1 answer

Can someone please explain the "indices trick"?

I noticed the "indices trick" being mentioned in the context of pretty-printing tuples. It sounded interesting, so I followed the link. Well, that did not go well. I understood the question, but could really not follow what was going on. Why do we…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
16
votes
2 answers

What does this variadic template code do?

template void for_each_argument(F f, Args&&... args) { [](...){}((f(std::forward(args)), 0)...); } It was recently featured on isocpp.org without explanation.
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
16
votes
1 answer

C++ - How to introduce overload set from variadic number of bases.

The derived class hides the name of an overload set from the base class if the derived class has the same name defined, but we can always introduce that overload set back with using-declaration: template class A : public BASE { public: …