How to know underlying type of class enum?

前端 未结 4 996
温柔的废话
温柔的废话 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: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  //include this
    
    FooEnum myEnum;
    auto pointer = static_cast::type*>(&myEnum);
    

提交回复
热议问题