Originally posted by: RoyHu
The XL C/C++ Compiler, V12.1 introduces support for the C++11 feature "scoped enumeration". This new feature is designed to resolve the following problems associated with the traditional enumeration:
Problem 1: Implicit conversion to an integer
Traditional C++ enumerations are not type-safe, because the value of an enumerator or an object of an enumeration type is converted to an integer by integer promotion. For example:
enum color { ClrRed, ClrOrange, ClrGreen, ClrYellow};
enum Alert { CndGreen, CndYellow, CndRed};
Color c = ClrRed;
Alert a = CndGreen
a = c; // error
a = ClrYellow; // error
bool armweapon = (a >= ClrYellow); // oops, implicit conversion to integer is allowed.
Problem 2: Inability to specify the underlying type
For traditional C++ enums, the underlying type is defined by implementation. You cannot specify the underlying type explicitly.
Problem 3: Scope issue
Traditional C++ enums do not have a strong scope. The enumerators of an enum are exported to the scope in which the enum is defined. For example, two enumerations in the same scope are not allowed to have enumerators with the same name.
The scoped enumeration feature can get you the following benefit:
• Improve support for library building and security by providing better type safety.
• Make C++ enumerations easy to learn and teach by removing common stumbling blocks that trip new programmers.
• Improve support for systems programming.
• Remove inconsistencies with traditional C++ enumerations.
• Reduce compile-time and dependencies with the introduction of forward declarations of enumerations.
For details about the scoped enumeration, you can access the following topics:
• IBM information center