36

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 value of the enum?

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

Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • 1
    @YochaiTimmer, standard changed and topic on the link does not tell about `enum class` – Lol4t0 Mar 24 '12 at 17:10
  • @YochaiTimmer: Not at all. I want to know if I can change enum to store its values internally with a `uint16_t`, not whether it is the same size as `int`. – Linuxios Mar 24 '12 at 17:14

4 Answers4

48

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • I was wondering that myself. The only difference is that `enum class` creates a new scope, whereas `enum` does not. – chris Mar 24 '12 at 17:26
  • 3
    @chris: No, there are several differences between `enum class` and `enum`. Both do create scopes (the above `Bar` can be accessed with `EnumType::Bar`), but only the `class` version *forces* you to use the scoped enum. Also, `enum class` variables do not *implicitly* convert integers to the enumeration type. So you must either pass an actual enumerator of the correct type, or you must perform an explicit cast. This adds a *lot* more type safety. – Nicol Bolas Mar 24 '12 at 17:30
  • Oh, I got my point from another question on SO: http://stackoverflow.com/questions/441552/scope-resolution-operator-on-enums-a-compiler-specific-extension As for your second point, I simply never heard about that one. Thanks for the free knowledge :) – chris Mar 24 '12 at 17:46
20

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 <type_traits> 

std::underlying_type<Foo>::type v = 10; //v is uint16_t
Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
6

With c++11 you now have enum class, that allows you to set underlying type explicitly:

enum class Foo: uint16_t 
{ 
   Bar,
   Baz,
   Bork,
}; 
Lol4t0
  • 12,444
  • 4
  • 29
  • 65
5

With pre-C++2011 you can force a minimum storage by using a suitable range of values:

enum foo {
    v0 = 0,
    vmax = 32767
};

I think the compiler is free to choose either a sign or an unsigned integer type as the underlying type. The above range enforced that the representation uses at least short as it underlying integer. Making it even one bigger may cause it to use long instead. Of course, this only forces a minimum range and the compiler is free to choose a bigger range. Also, with the above definition you are not allowed to stray outside the range [0, 32767]: if you really need a 16 bit range (at least) you need to use corresponding values).

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • 1
    Actually, there is a restriction on the size of the underlying type as well. [dcl.enum]/6 precises: except that the underlying type shall not be larger than `int` unless the value of an enumerator cannot fit in an `int` or `unsigned int`. – Matthieu M. Mar 24 '12 at 19:36