I’m very surprised to find that the following compiles:
#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;
}
Why is this allowed? It just seems wrong that class SomeCls<int> doesn’t need to have a void UseT(T t) method. I’m sure I’m missing the point of specialization here (I’m not a C++ expert). Can someone please enlighten me?
Specialisation can specialise in any way you see fit. If you want to omit all the methods, add or remove base classes, add or remove data members then that’s fine.
When specialising functions, the only restriction is that the arguments stay the same (with respect to the primary template), although you can overload to your heart’s content.