Is it possible to use variable as template parameter without switch or if else statements for each possible value?
enum A {a, b, c, d};
template<A> void f() {/* default */};
template<> void f<A::a>() {/* ... */}
template<> void f<A::b>() {/* ... */}
template<> void f<A::c>() {/* ... */}
void execute(A action) {
f<action>()
}
I could use switch statement.
void execute(A action) {
switch (action) {
case A::a:
f<A::a>();
break;
case A::b:
f<A::b>();
break;
case A::c:
f<A::c>();
break;
}
}
Or I could add function pointers to a map and use this map afterwards.
std::map<A, void(*)()> mp = {
{A::a, f<A::a>},
{A::b, f<A::b>},
{A::c, f<A::c>}
};
void execute(A action) {
mp[action]()
}
But both of these solutions require me to specify the mapping manually.
Is there a way of calling function based on a variable? Maybe using macro with function definition, or using template metaprogramming.