How to know underlying type of class enum?

前端 未结 4 999
温柔的废话
温柔的废话 2020-12-03 17:05

I have a variable declared as:

enum class FooEnum: uint64_t {}

and I would like to cast to its base-type, but I don\'t want to hardcode the

4条回答
  •  天涯浪人
    2020-12-03 17:09

    Both Visual C++ 10.0 and MinGW g++ 4.6.1 lack std::underlying_type, but both accept this code:

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

提交回复
热议问题