C++ Implementation-defined behavior Underlying type (and hence size) of an enum

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

If 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.



Got any C++ Question?