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

Error with variadiac template: "parameter pack must be expanded"

Here's a variadic template function that I've written: template Value& insert(Container& c, Args&&... args) { c.emplace_back(args); return c.back(); } When I use insert like this I'm getting an…
user2556165
24
votes
4 answers

Multiple typename arguments in c++ template? (variadic templates)

How can I have multiple typename arguments in a c++ template? #ifndef _CALL_TEMP_H #define _CALL_TEMP_H #include #include template class Foo; template class Foo { public: …
Jichao
  • 40,341
  • 47
  • 125
  • 198
23
votes
2 answers

How do I restrict fold expressions with C++ 20 requirements/concepts?

How do you restrict the allowed types in variadic templates and fold expression using C++20 concepts? For example suppose I'd like to restrict the following fold expression to only support integral types, how would I do that? #include…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
23
votes
8 answers

Variadic templates

I have seen a lot of links introducing the variadic templates. But I have never seen any compilable example that demonstrates this approach. Could someone provide me with some links in which such compilable examples can be found?
sami
  • 467
  • 2
  • 6
  • 11
23
votes
1 answer

Why does this exceed the maximum recursive template depth?

I've been playing around with variadic templates and noticed the following. This works fine: auto t = std::make_tuple(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16); This will give the error (gcc 4.8.2 (edit: Clang 3.4) has maximum depth of 256 by…
Jordan
  • 233
  • 2
  • 6
23
votes
5 answers

C++ index of type during variadic template expansion

I have a simple yet daunting problem I can't solve by myself. I have something like template T* create(SomeCastableType* args, size_t numArgs) { return new T(static_cast(args[INDEX_OF_EXPANSION])...); } Suppose…
keebus
  • 990
  • 1
  • 8
  • 15
23
votes
4 answers

variadic template of a specific type

I want a variadic template that simply accepts unsigned integers. However, I couldn't get the following to work. struct Array { template // this works // template -- this does not work (GCC 4.7.2) …
Zach Saw
  • 4,308
  • 3
  • 33
  • 49
23
votes
3 answers

How to use GCC's printf format attribute with C++11 variadic templates?

I have a C++ class that is the frontend for a logging system. Its logging function is implemented using C++11's variadic templates: template void Frontend::log(const char *fmt, Args&&... args) { backend->true_log(fmt,…
Marco Leogrande
  • 8,050
  • 4
  • 30
  • 48
22
votes
3 answers

Matching variadic non-type templates

Let's say I have two structs, Foo and Bar: template struct Foo{}; template struct Bar{}; I want to create a type trait (call it match_class) that returns true if I pass two Foo<...> types or two Bar<...> types, but false…
AndyG
  • 39,700
  • 8
  • 109
  • 143
22
votes
2 answers

Why does std::visit take a variable number of variants?

Trying to get more familiar with C++17, I've just noticed std::visit: template constexpr /*something*/ visit(Visitor&& vis, Variants&&... vars); Why does std::visit not take a single variant, but rather any number…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
22
votes
3 answers

Is there a case where vararg functions should be preferred over variadic templates?

Variadic templates have lot of advantages, but are there occasions when C-style variadic functions (using ) should be used instead?
Vladimir Yanakiev
  • 1,240
  • 1
  • 16
  • 25
22
votes
6 answers

How to write a variadic template recursive function?

I'm trying to write a variadic template constexpr function which calculates sum of the template parameters given. Here's my code: template constexpr int f() { return First + f(); } template constexpr…
alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51
22
votes
2 answers

Variadic Template conversion to std::function works with GCC and not MSVC2013, why?

If this is a duplicate I appologize. I couldn't find anything in my searches though. I can use any of the newest features c++11/c++14. I can upgrade to VS2015 if necessary. I'm trying to write a class that will auto cast into a std::function with a…
extracrispy
  • 669
  • 5
  • 16
22
votes
2 answers

What are the 6 dots in template parameter packs?

While looking at this question I found myself in the cpp reference site where I noticed a strange and new to me syntax : template struct is_function : std::true_type {}; Yep, 6 dots ! Initially…
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
22
votes
8 answers

effective way to select last parameter of variadic template

I know how to select first parameter of variadic template: template< class...Args> struct select_first; template< class A, class ...Args> struct select_first{ using type = A;}; It's a very simple. However, select_last is not…
Khurshid Normuradov
  • 1,564
  • 1
  • 13
  • 20