Enums in C++0x

This is good news for C++:  Herb Sutter reports that in July the ISO C++ committee voted better enum support into the language. 

It’s all good, but the part that’s affected me in the past is the scoping of enum values.  Previously:

1
 <p>class MyClass<br>{<br>&nbsp; enum&nbsp;BorderColor { Red, Green, Blue };<br>};</p>

The values Red, Green, and Blue were scoped to MyClass, not to BorderColor.  So you couldn’t say MyClass::BorderColor::Red, you had to say MyClass::Red.  If your class has a lot of enums in it, they all get scoped to the class and this just gets messy. 

Of course they can’t just change the language without risking compatibility so the new syntax will look like:

1
 <p>class MyClass<br>{<br>&nbsp; enum&nbsp;class BorderColor { Red, Green, Blue };<br>};</p>

I can live with that.

Check out the full details here.

One Response to “Enums in C++0x”

  1. Dr. Blip's PC-Doctor Blog Says:

    Enums in C++ Suck…

    Like most strongly typed languages, C++ has a way to group a set of constants together as their own type called enums. Enums are extremely useful in a wide variety of circumstances. However, enums in C++ have a lot of problems, and, in fact, they’re r…

Leave a Reply

Powered by WP Hashcash