I set that to /std:c++latest and it worked. But should I do that for each project individually? |
I don't know, I don't use Visual Studio, but I wouldn't be surprised if you can at least set the default options to be used for new projects, but that is just a guess.
Note that these rules apply to scoped enumerations (enum class), not necessarily to unscoped enumerations (enum). |
You mean that there we could assign a number, 6, which is not amongst the enum Color enumerators (0, 1, 2) to y because the enum was declared scoped, and if it were declared unscopped we would have to pick one number exactly among the defined package (enumerator constants). Right? |
If the
underlying type is not "fixed" the result is
undefined if you assign a value that is larger than what can be stored in the smallest bit field that are able to store all the enumerators. In this case the largest enumerator value is 2, which requires at least 2 bits to be stored. The largest possible value that you can store in 2 bits is 3, which means you would be able to store the values 0, 1, 2, 3, but not 4 and above.
The only situation when the underlying type is not fixed (and the above rules apply) is when you use an unscoped enum without specifying the underlying type. Scoped enums uses int as the underlying type by default so they always have a fixed underlying type. If the underlying type is fixed it means you can safely store any value that can be represented by the underlying type so that is why it is possible to store 6 in Color because 6 is small enough to be stored in an int.
1 2
|
struct SColor { int value; };
SColor sc1 { 5 } ; // OK
| |
|
Here I don't assume sc1 is an object where its int value is 5, because even if the compiler creates a default constructor for us, in the absence an explicit constructor, there's not any instruction to put the gotten value (5) into that int value of the struct. |
It creates a SColor object with the
value member initialized to 5.
This type of initialization is called
aggregate initialization. It's only possible if the class fulfills the requirements for being an aggregate (only public data members, no user-declared constructor, no virtual member functions, etc.).
https://en.cppreference.com/w/cpp/language/aggregate_initialization