How to know underlying type of class enum?

前端 未结 4 987
温柔的废话
温柔的废话 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:06

    Here is another approach for when underlying_type is not present. This method doesn't attempt to detect the signed-ness of the enum, just give you a type of the same size, which is more than enough for a lot of situations.

    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;
    };
    

    Usage:

    enum EFoo {Alpha, Beta};
    EFoo f = Alpha;
    TIntegerForEnum<EFoo>::type i = f;
    TIntegerForEnum<decltype(f)>::type j = f;
    
    0 讨论(0)
  • 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;
    };
    
    0 讨论(0)
  • 2020-12-03 17:28

    Your guessed syntax is amazingly close. You're looking for std::underlying_type in <type_traits>:

    #include <type_traits>
    #include <cstdint>
    
    enum class FooEnum: std::uint64_t {};
    
    int main()
    {
        FooEnum myEnum;
        uint64_t* intPointer = (std::underlying_type<FooEnum>::type*)&myEnum;
    }
    
    0 讨论(0)
  • 2020-12-03 17:29

    You can use this:

    • std::underlying_type class template to know the underlying type of enum.

    The doc says,

    Defines a member typedef type of type that is the underlying type for the enumeration T.

    So you should be able to do this:

    #include <type_traits> //include this
    
    FooEnum myEnum;
    auto pointer = static_cast<std::underlying_type<FooEnum>::type*>(&myEnum);
    
    0 讨论(0)
提交回复
热议问题