【发布时间】:2012-02-14 11:54:32
【问题描述】:
这是我的代码:
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;
}
效果很好。但是当我尝试添加这个
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;
}
我得到编译器错误: «>» 标记之前的无效显式特化 «int MyClass::DoSomething()» 的模板 ID «DoSomething» 与任何模板声明都不匹配
我使用 g++ 4.6.1 我该怎么办?
【问题讨论】: