I have a enum class:
enum class flipping_t {
eNone,
eHorizontal,
eVertical
};
Then I’m trying to use its values as a template parameters:
template<flipping_t>
void areas();
template<>
void areas<flipping_t::eHorizontal>();
template<>
void areas<flipping_t::eVertical>();
My app has a variable of type flipping_t:
auto flip = flipping_t::eNone;
I want to change its value depending on which key has just been pressed down (I’m working on simple photo editor so want to mirror a picture)
Then I’m invoking a function according to current flip:
areas<flip>();
And compiler says:
no instance of function template "areas" matches the argument
I understand why areas<flipping_t::eNone>() works fine. I’m looking for a way to fix areas<flip>()
>Solution :
A template parameter can be of an integral type, what flipping_t is, but is shall be a constant. That is the reason why areas<flipping_t::eNone>() works fine: areas<flipping_t::eNone>() is indeed a (compile time) constant.
But when you declare auto flip = flipping_t::eNone;, flip is a variable, and its values is allowed to change at run time. That is the reason why you cannot use it as a template parameter.
If it can be constant, you could say it to the compiler:
const auto flip = flipping_t::eHorizontal;
This is enough to later be able to use areas<flip>(). But in that case, flip is nothing more than an alias to flipping_t::eHorizontal