jeudi 25 mai 2017

compile time substitution in initializer list

I have a template class which is ensured to basically be only instantiated with 1 of 2 enums. Now I want to set the value of the template parameter in the initializer based on what enum it was instantiated with. Something like this:

enum class MyFirstEnum { red, green, orange };
enum class MySecondEnum { blue, yellow, red };

template <class T>
class MyClass
{
    static_assert(
        !(std::is_same<T, MyFirstEnum>::value || std::is_same<T, MySecondEnum>::value), 
        "Template parameter must be either MyFirstEnum or MySecondEnum"
    );
public:
    MyClass() 
        : value(std::is_same<T, MyFirstEnum>::value ? MyFirstEnum::red : MySecondEnum::blue)
    {
    }
private:
    T Value;

}

But of course the compiler complains since the types don't match and the ternary is not a compile time replacement. Is there a way to assign the correct value based on the type parameter?

Any help is appreciated. I am limited to c++11

Aucun commentaire:

Enregistrer un commentaire