【问题标题】:Underlying type of a C++ enum in C++03C++03 中 C++ 枚举的基础类型
【发布时间】:2014-10-01 18:34:41
【问题描述】:

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

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

【问题讨论】:

    标签: c++ std typetraits


    【解决方案1】:

    this 解决方案怎么样?

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

    你可以找到它的构建块(条件,make_signed,make_unsigned in boost::type_traits)

    【讨论】:

    • 这是在 C++11 编译器中实现underlying_tpye 的一种非常奇特的方式......但是,我确实有一个 C++03 编译器,没有条件或 make_signed
    • 我只是在想那个@sammy! make_signed 需要 C++11。对不起。我应该删除我的答案吗? ://
    • 那么make_signed@BillyONEal 呢? :/ 我想不出不使用 C++11 来实现它的方法。
    • 您的所有构造都可以在 Boost 中使用,而且我已经能够构建我的 UnderlyingType。谢谢!
    • @G.Samaras:您创建了一个主模板,将T 类型定义为type,并明确专门化charshortint 等,其中类型定义@987654330 @、unsigned shortunsigned int 等如type
    猜你喜欢
    • 2010-10-25
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多