How to know underlying type of class enum?

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

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

    #include 
    #include 
    
    enum class FooEnum: std::uint64_t {};
    
    int main()
    {
        FooEnum myEnum;
        uint64_t* intPointer = (std::underlying_type::type*)&myEnum;
    }
    

提交回复
热议问题