Sam
Sam

Reputation: 20058

Underlying type of a C++ enum in C++03

Is there a way to get the equivalent of std::underlying_type in C++03 compilers?

I'm aware of some support in boost::type_traits, but there is no fully functional converter there.

Upvotes: 0

Views: 440

Answers (1)

gsamaras
gsamaras

Reputation: 73366

How about this solution?

template< class TpEnum >
struct UnderlyingType
{
    typedef typename conditional<
        TpEnum( -1 ) < TpEnum( 0 ),
        typename make_signed< TpEnum >::type,
        typename make_unsigned< TpEnum >::type
        >::type type;
};

You can find the building blocks for it (conditional, make_signed, make_unsigned in boost::type_traits)

Upvotes: 2

Related Questions