C++ Variadic Function Templates of Known Type

c++, c++11, templates, variadic-functions, variadic-templates

Solution

This should work:

void foo(int);

template<typename ...Args>
void foo(int first, Args... more)
{
   foo(first);
   foo(more...);
}

Problem

I'm currently trying to get my head around some of the things I can do with variadic template support. Let's say I have a function like this - ``` template <typename ... Args> void foo(Args ... a) { int len = sizeof...(tail); int vals[] = {a...}; /* Rest of function */ } /* Elsewhere */ foo(1, 2, 3, 4); ``` This code works because I assume beforehand that the arguments will be integers, but obviously will fail if I provide something else. If I know that the parameter packs will contain a particular type in advance, is there some way that I can do without the templating and have something like - ``` void foo(int ... a) ``` I have tried doing this, but the compiler gave an error about foo being a void field. I know that I can also access the parameters in the pack through recursion, but I'm not sure this will fix the problem that I have - namely I want to be able to take a variable number of arguments of the same type.

Original source