given enum class Foo
enum class Foo : uint8_t
{
A, B, C
};
why does this cast fail?
uint8_t X = 1;
Foo& X_ref = *static_cast<Foo*>(&X);
but reinterpret cast does not fail?
uint8_t X = 1;
Foo& X_ref = *reinterpret_cast<Foo*>(&X);
I'm wondering if this cast is safe and I can safely assign enum values of Foo
to X_ref
and expect X to be changed accordingly as long as type of X is same as underlying type of enum class (in this case uint8_t
)