I want to write a template class with two template parameters, where one implementation of a method is available when the parameter types are the same, and a different implementation when the types are different.
I know I can do this with template specialization:
template<typename T, typename W> class MyClass {
public:
void myMethod() { std::cout << typeid(T).name() << " != " << typeid(W).name() << std::endl; }
};
template<typename T> class MyClass<T,T> {
public:
void myMethod() { std::cout << typeid(T).name() << " = " << typeid(T).name() << std::endl; }
};
But I was trying to get better at SFINAE paradigms, and so tried writing something like this:
template<typename T, typename W> class MyClass {
public:
template<typename T_ = T, typename W_ = W,
typename = std::enable_if_t<std::is_same_v<T_,W_>>> void myMethod() {
std::cout << typeid(T_).name() << " = " << typeid(W_).name() << std::endl;
}
template<typename T_ = T, typename W_ = W,
typename = std::enable_if_t<!std::is_same_v<T_,W_>>> void myMethod() {
std::cout << typeid(T_).name() << " != " << typeid(W_).name() << std::endl;
}
};
But the compiler says that this is redeclaration of the method.
Can someone correct my usage of SFINAE here?
Thanks!