I have a variable declared as:
enum class FooEnum: uint64_t {}
and I would like to cast to its base-type, but I don’t want to hardcode the base-type. For instance, something like this:
FooEnum myEnum;
uint64_t * intPointer = (underlying_typeof(myEnum))&myEnum;
Is this possible?
Since C++ 11 you can use this:
std::underlying_typeclass template to know the underlying type of enum.The doc says,
So you should be able to do this:
In C++ 14 it has been a bit simplified (note there is no
::type):And finally since C++ 23 one can get value without explicit cast (docs):