C++ Underlying type (and hence size) of an enum


If the underlying type is not explicitly specified for an unscoped enumeration type, it is determined in an implementation-defined manner.

enum E {
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.