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

High performance solution to take N arguments and return N values

What could I use to make a function take N number of arguments, where N is not know at programming time but is fixed at compile time (it is template parameter, in fact)? The function in question is an access function which lies in a performance…
TeaOverflow
  • 2,468
  • 3
  • 28
  • 40
3
votes
2 answers

Function that accepts variadic template list of brace constructed objects

Hi there template meta programming experts. I'm attempting to write a (class member) function the could ideally take as an argument what might be called a type agnostic map. Ideally something like: foo({"Bar", 42}, {"Baz", "Blargh"); as well…
kingguru
  • 33
  • 3
3
votes
1 answer

variadic struct specification

I define a variadic struct like so template struct Opa { Opa() { std::cout << "Mutiple-arguments template"; } }; and want to specialize it for the case with 1 argument only as follows template <> struct…
Alex B.
  • 348
  • 2
  • 12
3
votes
1 answer

C++11 Code::Blocks GCC crashes when compiling variadic template of dependent member structs

I was testing an idea with variadic templates in C++ using Code::Blocks, and when I try to compile it, the build fails and says: ' in dependent_type_p, at cp/pt.c:19367 Please submit a full bug report, with preprocessed source if appropriate. See…
3
votes
2 answers

Can't add perfect forwarding to wrapper function

While answering this question I wrote this working code, wrapping function passed in template arguments: template auto wrapper(Args... args)->decltype(fn(args...)){ return fn(args...); } #define…
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
3
votes
3 answers

Restrict supported types of a template class by using variadic templates

I'm trying to handle image processing operation classes which are only compatible for certain combinations of: a set of dimensions [1,2,3, ...] a set of types [int, float, double, ...] One working method would be to define a generic template…
qCring
  • 1,422
  • 1
  • 15
  • 20
3
votes
0 answers

Generic way to pass parameters to pthread_create (with variadic template function with 2 packed-parameters lists)

I'm trying to implement a thread class as the C++11 standard defines, by myself, as I found in this a good way to work with variadic templates and other advanced features of the C++ language. As far as I know, when using the pthreads library, when…
abidon
  • 456
  • 4
  • 15
3
votes
1 answer

Acceptable way to restrict variadic templates in constructors

To explain my question, I'll first paste some example code then ask the related question. template< typename... CONDITIONS > struct all_true; template<> struct all_true<> { const static bool value = true; }; template< typename CONDITION,…
qeadz
  • 1,476
  • 1
  • 9
  • 17
3
votes
2 answers

Remove last parameter from a parameter pack

Today, while using the same template for the hundredth time, I came across the idea to introduce a simple replacement. Instead of QHash > > it should now be: MultiKeyHash (Not to be confused with…
Timo
  • 1,724
  • 14
  • 36
3
votes
1 answer

Expand a parameter pack with a counter

I want to expand a paramter pack into a function call, like this: func(get_value(arg)...); where func and get_value are two functions. The question is that get_value is sensitive to evaluation order, so I think one solution is to generate something…
3
votes
5 answers

Calculate the average of several values using a variadic-template function

I am trying to write a function to determine the average of an arbitrary number of arguments, all of which have the same type. For learning purposes I am trying to do this using a variadic-templated function. This is what I have so…
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
3
votes
5 answers

How to write a c++ assert macro with a varying number of informational arguments?

I am trying to write a macro dbgassert similar to the standard assert. In addition to what assert does, I want to dbgassert print an arbitrary number of additional parameters (containing debugging information). What I have so far is listed below,…
thor
  • 21,418
  • 31
  • 87
  • 173
3
votes
1 answer

Tagged dispatch with variadic templates, c++11

I'm trying to make a function accept different arguments depending on the enum. // cake.h #pragma once #include enum class TYPE { CupCake, Jelly, BirthdayCake }; struct cake_tin { /* etc etc */ }; /** Fills in different ingredients for…
dpington
  • 1,844
  • 3
  • 17
  • 29
3
votes
2 answers

Why does gcc complain "error: type 'intT' of template argument '0' depends on a template parameter"?

My compiler is gcc 4.9.0. The following code cannot be compiled: template struct value {}; template struct value {}; // error: type 'T' of template argument '0' depends on a template parameter What is the cause?…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
3
votes
2 answers

Can this not be done with variadic template parameter packs?

In the code below I expand a parameter pack inside an initializer list, while calling a function DoSomethingReturnInt on each element. Below that I attempt to do something seemingly similar to try and call DoSomething on each element, but get a…
Chris_F
  • 4,991
  • 5
  • 33
  • 63