How to change the integer type used by an enum (C++)?

前端 未结 4 2005
既然无缘
既然无缘 2021-01-03 18:49

If I have an C++ enum:

enum Foo
{
  Bar,
  Baz,
  Bork,
};

How do I tell the compiler to use a uint16_t to actually store the

4条回答
  •  粉色の甜心
    2021-01-03 19:03

    In C++11, you can do that:

    enum class Foo : uint16_t 
    {
      Bar,
      Baz,
      Bork,
    };
    

    Later on, you can also know the underlying type of the enum as:

    #include  
    
    std::underlying_type::type v = 10; //v is uint16_t
    

提交回复
热议问题