How can I make a template constant value of any type to be accepted in C++? Example:
using namespace std;
typedef enum { E11, E12} E1;
typedef enum { E21, E22} E2;
void a(E1 e) { cout<<"something from E1"<<endl; }
void a(E2 e) { cout<<"something from E2"<<endl; }
template <typename T, T Element>
class Foo {
public:
void foo() {
a(Element);
};
};
// Unwanted "solution" using #define
#define _Foo(x) Foo<decltype(x), x>
int main() {
Foo<E1, E12> d1;
d1.foo();
Foo<E2, E22> d2;
d2.foo();
// using define-solution
_Foo(E12) d3;
d3.foo();
_Foo(E22) d4;
d4.foo();
}
See the hacky solution using define, but what I really want is:
template <AnyTypeAcceptor Element>
class Foo {
public:
void read() {
a(Element);
};
};
int main() {
Foo<E22> d1;
d1.read();
Foo<E11> d2;
d2.read();
}
As you can see above with the define solution all the information necessary is definitely here at compile time but I suspect there is no solution in C++?