C++ enum sizes

Dec. 5, 2016, 10:27 p.m.

How big are C++ enums?

It would be a very boring note if I said 4 bytes. Yes, it can be more than or less than 4, the only requirement in the standard is that the enum can hold all of its enumerated values. That is, different enum types may have different sizes.

Obviously, in practice most sane compilers nowadays would default to 4 bytes. But it's simple to nudge an enum beyond an int.

enum Big {
    BIG_ELE = 1LL << 32
assert(sizeof(BIG_ELE) == 8);

It works even with gcc's weird __int128 type!

enum Hueg {
    HUEG_ELE = (__int128)1 << 64
assert(sizeof(HUEG_ELE) == 16);

C++11 enum classes also supports this, but only if you specify a data type for the enum class (otherwise, it will assume int by default, and throw a compiler error).