Hello, I’m Liang Tang.

This is part 14 of the EasyC++ series. Let’s talk about enumerations in C++.

If you want a better reading experience, you can visit the Github repository.

The enumeration

Introduction to the

Enumerations are provided in C++, and we can create enum types using the enum keyword.

This creates symbolic constants instead of const keywords. You can also customize names to make your code more readable. Such as:

enum color {red, blue, orange, white, black};
Copy the code

We do two things in this statement. First we create a new variable type called color, which is an enumeration type. Second, we created some symbolic constants, such as red,bule,orange, etc. By default, these enumerators are assigned integers, with the first enumerator red being 0, the second one blue being 1, and so on.

Of course we can also explicitly assign values to these enumerators, as in:

enum color {red=3, blue=1, orange, white, black};
Copy the code

Red and blue will be assigned the values we gave them, and then orange will be assigned 2, 3, and 4.

use

Once we define an enumerated type, we can declare it as a normal type:

color a;
Copy the code

Since color is an enumerated type, when we assign, we can only assign the enumerated type, and attaching other values may cause problems. The results vary depending on the compiler, with some reporting errors and others just warning. This is not what we should do, either to report errors or to warn:

color a = red;	// OK
color a = 10;	// Report an error or warning
Copy the code

Because the enum underlying store is an integer, some strange operations are allowed, but they are strongly discouraged and can be risky. Such as:

cout << (red < blue) << endl;	// Compare the sizes
cout << blue - red << endl;	// Add and subtract

int c = red + 3;	// Assign to int
Copy the code

These operations are syntactically permissible, but are highly discouraged because they make no sense. Each of the enumerated types has its own logical meaning and cannot be used for calculation. Although grammatically possible, it makes no logical sense.

We can also cast an integer to an enumerated type:

color c = color(3);
Copy the code

Again, it is not recommended because it is possible that the enumerator corresponding to the number 3 does not exist, which will not report an error, but may affect the correctness of the program.

The value range of an enumeration value

As mentioned earlier, only enumerated values in declarations are valid. However, since C++ allows casting to enumeration values, in theory any value in the range of enumerated values can be converted to enumeration values, even though they may not make logical sense.

For enumeration variables, the range is not fixed, but fluctuates according to the definition. C++ calculates the upper and lower limits based on enumeration declarations, and only allows integer values in the range to be forced into enumeration values.

enum flag {black = 1, white = 2, red = 23};
Copy the code

C++ takes the minimum-length approach. For example, the enumeration we defined above has a maximum of 23, which computes the lowest power of 2 greater than 23, which is 32. So the upper limit of this enumeration value is 31, and a similar calculation is used for the lower limit, which is 0 if the minimum value defined is greater than or equal to 0, otherwise the same algorithm is used with a minus sign.

The reason for this complexity is to save as much memory as possible. After all, a lot of C++ programs are running environment is microcontroller or chip, memory is not abundant.