enumeration cannot be a template
is the error given when I try to compile with BCC64 (based on Clang) the following code:
template <typename T> enum class fooEnum : T
{
a,b,c,d,e
};
At first, I was thinking that this explicit prohibition was due to the enum
underlying type limitations, if the enum underlying type could be templated, then it could lead to ill-formed enums, but when we try this:
template <typename A> struct fooClass
{
enum class fooEnum : A
{
a,b,c,d,e
};
};
It compiles without problem as long as the A
type follows the same limitations as the enum underlying types, you know, the expression that defines the value of an enumeration:
- Shall be an integer constant large enough to fit all the values of the enum
- Each enumerated type shall be compatible with
char
or asigned
/unsigned
integer type.
If we do not not follow this rules, (with an in-class or a global enum) another specific error shows up, as expected:
enum class fooEnum : fooClass
{
a,b,c,d,e
};
non-integral type 'fooClass' is an invalid underlying type
So, that's why I'm wondering why is explicitly forbidden to create a template enum even when there is already a control over the underlying type. Where on the standard is mentioned this prohibition?
Thanks for your attention.