Underlying type and size of an enum
suggest changeIf the underlying type is not explicitly specified for an unscoped enumeration type, it is determined in an implementation-defined manner.
enum E {
RED,
GREEN,
BLUE,
};
using T = std::underlying_type<E>::type; // implementation-defined
However, the standard does require the underlying type of an enumeration to be no larger than int
unless both int
and unsigned int
are unable to represent all the values of the enumeration. Therefore, in the above code, T
could be int
, unsigned int
, or short
, but not long long
, to give a few examples.
Note that an enum has the same size (as returned by sizeof
) as its underlying type.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents