Enumeration

An enumeration is a distinct type whose value is restricted to a range of values.


We use enumeration type because it can be more comprehensive and readable.

Example:

enum Color {red, green, blue}; // an unscoped enum

Color r = red;
switch (r) {
	case red: ...
	case green: ...
	case blue: ...
}

Underlying type

Specify the underlying type:

enum class Color : char {red, yello, green=20, blue};

Unscoped

enum Foo {a, b, c=10, d, e=1, f, g=f+c}
// a=0, b=1, c=10, d=11, e=1, f=2, g=12
int num = c // c is 10

Scoped

Declaring a scoped enumeration type whose underlying type is int (the keywords class and struct are exactly equivalent)

enum struct|class name {enumerator = constexpr, enumerator = constexpr , ...}


For example,

enum class Color { red, green=20, blue};
Color r = Color::blue;

The values can also be converted to their underlying type but explicitly:

int n = Color::blue; // NOT OK
int m = (int) Color::blue; // OK
int l = static_cast<int>(Color::blue) // OK

Unscoped vs scoped

Unscoped enum type could be misused:

enum Color { red, yellow, blue };
enum MyColor { myblue, myyellow, myred };

Color col = red;
if (col == myred) { // should this be true?
}