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

前端 未结 4 1979
既然无缘
既然无缘 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:00

    You cannot do this in C++98/03. C++11 does allow you to do it, and without enum class the way everyone else seems to keep telling you:

    enum EnumType : uint16_t
    {
      Bar,
      Baz,
      Bork,
    };
    

    Again, you don't have to use enum class. Not that it's a bad idea, but you don't have to.


    Does GCC support this feature in its implementation of C++11?

    Which version of GCC? It looks like GCC 4.4 added this functionality, but you should probably look to more recent versions, just for the sake of stability.

提交回复
热议问题