If you know the types earlier, you can use function overloading with std:initializer_list :
#include <initializer_list> #include <iostream> void foo( std::initializer_list<int> l ) { for ( auto el : l ) // do something } void foo( std::initializer_list<float> l ) { } void foo( std::initializer_list<std::string> l ) { } int main() { foo( {1, 2, 3, 4 } ); foo( {1.1f, 2.1f, 3.1f, 4.1f } ); foo( { "foo", "bar", "foo", "foo" } ); return 0; }
If you are using Visual Studio 2012, you may need the Visual C ++ Compiler November 2012 CTP .
EDIT: If you still want to use a variation pattern, you can:
template <int ... Args> void foo( ) { int len = sizeof...(Args); int vals[] = {Args...};
But you must remember that it does not work with float and std::string , for example: you end up with 'float': illegal type for non-type template parameter . float not legal as a non-type template parameter , this is due to accuracy, floating point numbers cannot be represented exactly, and the likelihood that you are referring to the same type may depend on how the number is represented.
source share