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<int>
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<typename T>
struct TIntegerForEnum
{
typedef typename TIntegerForSize<sizeof(T)>::type type;
};
Usage:
enum EFoo {Alpha, Beta};
EFoo f = Alpha;
TIntegerForEnum<EFoo>::type i = f;
TIntegerForEnum<decltype(f)>::type j = f;