Here is my code:
template<typename T1, typename T2> class MyClass
{
public:
template<int num> static int DoSomething();
};
template<typename T1, typename T2> template<int num> int MyClass<T1, T2>::DoSomething()
{
cout << "This is the common method" << endl;
cout << "sizeof(T1) = " << sizeof(T1) << endl;
cout << "sizeof(T2) = " << sizeof(T2) << endl;
return num;
}
It works well. But when I try to add this
template<typename T1, typename T2> template<> int MyClass<T1, T2>::DoSomething<0>()
{
cout << "This is ZERO!!!" << endl;
cout << "sizeof(T1) = " << sizeof(T1) << endl;
cout << "sizeof(T2) = " << sizeof(T2) << endl;
return num;
}
I get compiller errors:
invalid explicit specialization before «>» token
template-id «DoSomething<0>» for «int MyClass::DoSomething()» does not match any template declaration
I use g++ 4.6.1
What should I do?
Unfortunately, you can’t specialise a template that’s a member of a class template, without specialising the outer template:
I think your best option is to add the extra parameter to
MyClass, and then partially specialise that.