Monday, April 23, 2012

C++0x: Variadic templates

Can I use variadic templates without using the template parameters as function parameters?



When I use them, it compiles:



#include <iostream>
using namespace std;

template<class First>
void print(First first)
{
cout << 1 << endl;
}

template<class First, class ... Rest>
void print(First first, Rest ...rest)
{
cout << 1 << endl;
print<Rest...>(rest...);
}

int main()
{
print<int,int,int>(1,2,3);
}


But when I don't use them, it doesn't compile and complains about an ambiguity:



#include <iostream>
using namespace std;

template<class First>
void print()
{
cout << 1 << endl;
}

template<class First, class ... Rest>
void print()
{
cout << 1 << endl;
print<Rest...>();
}

int main()
{
print<int,int,int>();
}


Unfortunately the classes I want to give as template parameters are not instantiable (they have static functions that are called inside of the template function).
Is there a way to do this?





No comments:

Post a Comment