Start at work to use a compiler that supports the standard C++17.
Actively we begin to use various features from this standard..
I was pleased with the [[fallthrough]] attribute for switch case constructions.
The essence of this attribute is that it indicates that the break statement was intentionally omitted in the switch case construction and that this is not a programmer's mistake.
The fact is that the switch case construction allows the end-to-end execution of the program code through all the choices of case, if the break statement has not been added.
Here is the classic version of this design.
int x = 100; bool check_x() { switch (x) { case 0: return true; case 50: x = 0: break; case 100: return false; default: return false; } }
But the variant with the end-to-end execution of the code when the break statement is omitted.
int x = 100; bool check_x() { switch (x) { case 0: case 50: case 100: return true; default: return false; } }
In this case, the same code will be executed for all values 0, 50, 100. But at the same time, the compiler may issue a prerequisite, and other programmers who may support your program code in the future may consider this an error.
To indicate that the omission of the break statement was made intentionally, the fallthrough attribute was entered,. And then this program code will look like this.
int x = 100; bool check_x() { switch (x) { case 0: [[fallthrough]]; case 50: [[fallthrough]]; case 100: return true; default: return false; } }
You can also inform the compiler that the default operator was also intentionally omitted.
int x = 100; bool check_x() { switch (x) { case 0: [[fallthrough]]; case 50: [[fallthrough]]; case 100: return true; case 105: [[fallthrough]]; } return false; }
In general, a very useful thing, which removes discrepancies and the human factor.