2012-02-24 CPlusPlus0X

Strongly typed enums

The old enum definition was a bit lame. Enums were almost interchangeable with an int and still not. This could cause some problems. With C++11 these problems have been resolve by introducing the enum class. An enum can be defined like this:

enum class Fruit
{
  Apple,
  Banana,
  Kiwi = 100,
  Pear /* = 101 */
};

That new type has the following properties:

- It is type safe. The type is not implicitly converted to int nor comparable with int. If you want to turn such an enum into an int you need to use a cast. - The underlying type of the enum is known and can even be specified. Default type is int. - The scope of the enumerators are within the enumeration. So enumerations defined at the same scope can have enumerators with the same name.