C/C++ and Fortran

C/C++ and Fortran

Connect, learn, share, and engage with IBM Power.

 View Only

The XL C/C++ V12.1 compiler supports scoped enumeration

By Archive User posted Sun March 17, 2013 10:53 AM

  

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
 •        IBM developerWorks                     
          http://www.ibm.com/developerworks/rational/library/scoped-enums/index.html
 
0 comments
0 views

Permalink