2

I would like to know how.

I have looked at this topic, and I understand that "The choice of type is implementation-defined.", but I am curious to know how to get 1 instead of 4.

Community
  • 1
  • 1
wagashi
  • 894
  • 3
  • 15
  • 39

2 Answers2

8

C++11 introduced a way to change the underlying type of an enum.

enum foo : char { ... };
enum class foo : char { ... };

Still, you're probably better off with using the default int in most cases.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
1

On GCC, you can also use the 'packed' attribute to tell the compiler you care more about space than word alignment / access speed:

enum foo { ... } __attribute__((packed));

There are similar hints for other compilers too.

(This is useful when trying to avoid any C++11 features that aren't yet supported by your compiler or IDE.)

dpercy
  • 459
  • 6
  • 13