Вот еще один подход, когда базовый_тип отсутствует. Этот метод не пытается обнаружить подпись перечисления, просто дает вам тип того же размера, которого более чем достаточно для многих ситуаций.
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;
};
Использование:
enum EFoo {Alpha, Beta};
EFoo f = Alpha;
TIntegerForEnum<EFoo>::type i = f;
TIntegerForEnum<decltype(f)>::type j = f;