我很惊讶地发现以下编译:
#include <iostream>
using namespace std;
template<typename T>
class SomeCls {
public:
void UseT(T t) {
cout << "UseT" << endl;
}
};
template<>
class SomeCls<int> {
// No UseT? WTF?!??!?!
};
int main(int argc, char * argv[]) {
SomeCls<double> d;
SomeCls<int> i;
d.UseT(3.14);
// Uncommenting the next line makes this program uncompilable.
// i.UseT(100);
return 0;
}
为什么允许这样做?class SomeCls<int>
不需要方法似乎是错误的void UseT(T t)
。我确定我在这里错过了专业化的重点(我不是 C++ 专家)。有人可以启发我吗?