Converting to an enum with no fixed underlying type out of "gamut"

94 views Asked by At

If you have an enum with no fixed underlying type, you can get its underlying type via std::underlying_type<E>.

enum no_fixed_underlying { bob = 1; };
std::underlying_type_t<no_fixed_underlying> x; // valid

Once you have done so, is it still considered not a fixed underlying type?

An enum without a fixed underlying type has a limited range, basically described as "the range of bits that could represent the values of the enum". Conversion to the enum with a value that is "outside those bits" is undefined behavior.

So, given an enum with no fixed underlying type, it seems you cannot convert all values of the underlying type to the enum value.

enum no_fixed_underlying { bob = 1; };
std::underlying_type_t<no_fixed_underlying> x = -1; // valid
auto e = (no_fixed_underlying)x; // undefined behavior!

Is this correct?

In the latest draft, expr.static.cast/10 and dcl.enum/8 are what I based the above understanding on.

0

There are 0 answers