Here is a simple example (the idea is to avoid a huge if..else
or switch
block inside a function):
namespace foo {
enum class VALUES : int { val_0 = 0, val_1 = 1 };
template<VALUES T>
void print();
template<>
void print<VALUES::val_0>() { std::cout << "val_0\n"; }
template<>
void print<VALUES::val_1>() { std::cout << "val_1\n"; }
void bar(int type) {
VALUES v = static_cast<VALUES>(type);
print<v>(); //error C2971 - "v" is a non-constant argument at compile-time
}
};
The question is how to call print<...>()
based on the bar
's parameter?
Please do not suggest to use if..else
or switch
.