The first syntax is ok, as a specialization. The second is wrong as it is defining a template<int>
while myMethod
is declared as a template<typename>
. But keep in mind that a specialization isn't needed at all, an overload is just fine, like
class MyClass {
void myMethod(const int& obj) { ... }
// ... more overloads
};
Of course, if you need to separate declarations from definitions (which I avoid for templates), this leads to more code duplication. Depending on type, this way you may also enforce call by value, e.g.
void myMethod(bool obj) { ... }
which does not apply to your specializations.