You are right.
Before C++11, all enums were just, basically, integers. And you could use them like that. It made it too easy to give bad values to functions expecting a restricted set of values. For example:
1 2 3 4 5 6 7 8 9 10
|
enum round_mode { round_half_up, round_half_down, round_bankers };
double round( double x, round_mode = round_half_up )
{
...
};
int main()
{
double x = round( 2.5, 42 );
| |
It compiles, but it isn't pretty.
With C++11, the compiler now knows all kinds of things about your enums, and doesn't let you blithely mix them with incorrect values.
Essentially, it promotes an enum to a first-class object -- it isn't just an integer.
The other issue is that the name for each enumeration bleeds into the containing scope. So the following would be a name conflict:
1 2
|
enum color_masks { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF };
int red = 0xFF0000;
| |
You can't have both the identifier 'red' as an enum value and as an integer variable name in the same scope.
While the example here is contrived, it isn't too far off from things that happen all the time -- and that programmers have to take pains to avoid.
(Some identifier names are common. For example, 'max'. If you #include <windows.h>, there's a 'max' macro in there, which plays havoc if you also #include <algorithm> and try to use the 'max' function, or #include <limits> and try to find the
numeric_limit <int> ::max()
. I know that's a macro problem, but it's the first name conflict I could come up with...)
There's more to read here:
http://www.stroustrup.com/C++11FAQ.html#enum
Hope this helps.