I'm trying to distinguish class implementation based on enum as a template argument. But, I want the class to have common function as below. The reason is that the common function behaves independent of the enum value. The code cannot be compiled because I'm doing something wrong. How can we resolve this?
#include <iostream>
enum E{
E1,
E2
};
template <enum E>
class X {
public:
void f_common() {
std::cout << "common" << std::endl;
}
};
template <>
class X<E::E1> {
public:
void f() {
std::cout << "E1" << std::endl;
}
};
template <>
class X<E::E2> {
public:
void f() {
std::cout << "E2" << std::endl;
}
};
int main() {
X<E::E1> e1;
X<E::E2> e2;
e1.f(); // "E1" is shown
e2.f(); // "E2" is shown
e1.f_common();// compile error
e2.f_common();// compile error
return 0;
}