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

A better LOG() macro using template metaprogramming

A typical LOG() macro-based logging solution may look something like this: #define LOG(msg) \ std::cout << __FILE__ << "(" << __LINE__ << "): " << msg << std::endl This allows programmers to create data rich messages using convenient and…
Marc Eaddy
  • 1,762
  • 3
  • 16
  • 23
32
votes
0 answers

What does six dots mean in variadic templates?

The following are some partial specializations for std::is_function from libstdc++'s : /// is_function template struct is_function : public false_type { }; template
chys
  • 1,546
  • 13
  • 17
31
votes
5 answers

Non-type variadic function templates in C++11

I saw a blog post which used non-type variadic templates (currently not supported by gcc, only by clang). template struct MultiDimArray { /* ... */ }; The example in the post compiles fine but I failed to get it to…
Motti
  • 110,860
  • 49
  • 189
  • 262
31
votes
2 answers

Quick sort at compilation time using C++11 variadic templates

I just implemented the quick sort algorithm by using C++11 variadic templates to evaluate it at compilation time. However, I encounter a performance issue when the data set is too large. #include using namespace std; template
Yuncy
  • 761
  • 1
  • 9
  • 20
31
votes
7 answers

Get types of C++ function parameters

Is there a standard way to get the types of a function's arguments and pass around these types as a template parameter pack? I know that this is possible in C++ because it has been done before. I was hoping that with C++14 or the upcoming C++1z,…
Navin
  • 3,681
  • 3
  • 28
  • 52
31
votes
5 answers

Calling a function for each variadic template argument and an array

So I have some type X: typedef ... X; and a template function f: class void f(X& x_out, const T& arg_in); and then a function g: void g(const X* x_array, size_t x_array_size); I need to write a variadic template function h that does…
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
30
votes
2 answers

using declaration in variadic template

This question is inspired in the following solution to multiple inheritance overloading pseudo-ambiguity, which is a nice way to implement lambda visitors for boost::variant as proposed in this answer: I want to do something like the…
lurscher
  • 25,930
  • 29
  • 122
  • 185
30
votes
2 answers

Lowest common ancestor in a linear lineage of types

Intro Let's suppose that we have a linear hierarchy of types like the following: Then what I want is a mechanism to return the lowest common ancestor out of an arbitrary number of types in that lineage. Attempted…
29
votes
9 answers

Can I implement max(A, max(B, max(C, D))) using fold expressions?

While trying to play around with C++17 fold expressions, I tried to implement max sizeof where result is maximum of the sizeof of types. I have an ugly fold version that uses variable and a lambda, but I am unable to think of a way to use fold…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
29
votes
1 answer

Are variadic constructors supposed to hide the implicitly generated ones?

Are variadic constructors supposed to hide the implicitly generated ones, i.e. the default constructor and the copy constructor? struct Foo { template Foo(Args&&... x) { std::cout << "inside the variadic…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
29
votes
2 answers

Can parameter pack function arguments be defaulted?

This is a point about which gcc 4.9.2 and clang 3.5.2 are in sharp disagreement. The program: template int foo(int i = 0, Ts &&... args) { return i + sizeof...(Ts); } int main() { return foo(); } compiles without comment…
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
28
votes
4 answers

C++11 variadic std::function parameter

A function named test takes std::function<> as its parameter. template void test(std::function f) { // ... } But, if I do the following: void foo(int n) { /* ... */ } // ... test(foo); Compiler(gcc 4.6.1)…
Daniel K.
  • 947
  • 3
  • 11
  • 21
28
votes
1 answer

Parameter pack expansion with lambda in C++20

Case 1: Consider the following pack expansion in lambda noexcept specifier: template auto g() { ([]() noexcept(B) {}, ...); } Clang and MSVC accept this code, but GCC rejects with: error: expansion pattern '' contains no…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
28
votes
2 answers

Lambdas in variadic templates

Using Microsoft Visual C++ 2013 (12.0), I am encountering compile-time errors when using a lambda in a constructor in a variadic template. I have managed to boil it down as shown below (see the lines with the error comments). It appears to be a bug…
Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
28
votes
2 answers

Parameter pack must be at the end of the parameter list... When and why?

I don't get the reason for which a parameter pack must be at the end of the parameter list if the latter is bound to a class, while the constraint is relaxed if the parameter list is part of a member method declaration. In other terms, this one…
skypjack
  • 49,335
  • 19
  • 95
  • 187