Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Enum variable as a template parameter

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading