lundi 4 mai 2020

C++11 How to cast enum class template parameter to int?

I have a struct wich takes enum class value on the template parameter.

template <typename EnumValue>
struct Type
{
public:
    static constexpr int VALUE = static_cast<int>(EnumValue);
};

enum class MyEnum {Val1, Val2};

std::cout << Type<MyEnum::Val2>::VALUE << std::endl;

I expected it to work, but it gives errors:

error: expected primary-expression before ‘)’ token
     static constexpr int VALUE = static_cast<int>(EnumValue);

error: type/value mismatch at argument 1 in template parameter list for ‘template<class EnumValue> struct Type’
  std::cout << Type<MyEnum::Val2>::VALUE << std::endl;

How to fix these errors without changing the template parameters for Type?

Since I don't want the template inputs to be changed, I don't want to use something like this:

template <typename EnumType, EnumType EnumValue>
struct Type...

Aucun commentaire:

Enregistrer un commentaire