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
Here is another approach for when underlying_type is not present. This method doesn't attempt to detect the signed-ness of the enum, just give you a type of the same size, which is more than enough for a lot of situations.
template
class TIntegerForSize
{
typedef void type;
};
template<>
struct TIntegerForSize<1>
{
typedef uint8_t type;
};
template<>
struct TIntegerForSize<2>
{
typedef uint16_t type;
};
template<>
struct TIntegerForSize<4>
{
typedef uint32_t type;
};
template<>
struct TIntegerForSize<8>
{
typedef uint64_t type;
};
template
struct TIntegerForEnum
{
typedef typename TIntegerForSize::type type;
};
Usage:
enum EFoo {Alpha, Beta};
EFoo f = Alpha;
TIntegerForEnum::type i = f;
TIntegerForEnum::type j = f;