The problem here is that EClass::A
is not a type; it's a value of type EClass
. Enum values in C++ are not treated as types, so you can't specialize a template based on them directly.
If you want to map enum values to corresponding names, you can take a different approach, such as using a function or a variable template instead of a class template specialization.
Here's an example using a constexpr function:
enum class EClass { A, B, C, D, E };
constexpr std::string_view getName(EClass e) {
switch (e) {
case EClass::A: return "A";
case EClass::B: return "B";
case EClass::C: return "C";
case EClass::D: return "D";
case EClass::E: return "E";
}
return ""; // or throw an exception, or handle it some other way
}
Alternatively, you can also use a variable template specialization:
enum class EClass { A, B, C, D, E };
template <EClass e> struct S;
template <> struct S<EClass::A> {
static constexpr std::string_view name = "A";
};
// More specializations...
Here, the template parameter e
is a non-type template parameter, and it's specialized based on the value EClass::A
, which is allowed.