C++03中C++枚举的基础类型

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

本文关键字:类型 枚举 C++ C++03      更新时间:2023-10-16

有没有办法在C++03编译器中获得std::underlying_type的等效值?

我知道boost::type_traits中有一些支持,但那里没有功能齐全的转换器。

这个解决方案怎么样?

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

您可以找到它的构建块(conditional,make_signed,make_unsigned in boost::type_traits)