0

Just curious. I wanted to do a bitmask for 8 values, but whenever I evaluate it, it always ends up being 32 bits. I tried

enum Foo {
        A = quint8(0)
}
sizeof(A); // 4 bytes, 32 bits
sizeof(quint8(0)); // 1 byte, 8 bits

I thought because it was possible to make 64 bit enumerators, that the opposite was true, that you could make ones that were only 8 bits.

Is it possible?

Botje
  • 26,269
  • 3
  • 31
  • 41
Anon
  • 2,267
  • 3
  • 34
  • 51

2 Answers2

3

You need to use the C++11 syntax for declaring the underlying type of an enum. Your attempt at restricting the enum values is undone by integer promotion.

enum Foo : uint8_t {
        A = 0,
};
Botje
  • 26,269
  • 3
  • 31
  • 41
2

An enum with an undefined underlying type always use a type that is at least as wide than an int.

Since C++11, you can specify the underlying type used by the enum:

enum Foo : quint8 {
 A = 0,
};
Nicolas Dusart
  • 1,867
  • 18
  • 26