C++11: enumeration type of scope

Posted by dswain on Thu, 27 Jan 2022 20:26:14 +0100

Enumeration type is a data type that organizes a limited set of integer constants to describe the value range of variables. There are two types of enumerations in C + +: scopeless enumerations and scoped enumerations. The scope limiting enumeration type is a new type introduced by C++11 standard.

● the restricted scope enumeration type appears to make up for the deficiency of the unrestricted scope enumeration type. The unrestricted scope enumeration type is not type safe, which is mainly reflected in the following aspects:

● enumeration members in enumeration types that do not limit the scope are regarded as integers, and two different enumeration types can be compared. Comparing two different types of data may lead to data type conversion and incomplete data representation.

● the integer type and its size used by the unrestricted scope enumeration are defined by the implementation method and cannot be specified explicitly.

● the scope is not limited. The enumeration members of the enumeration type and the external data of the enumeration type are in the same scope. Multiple enumeration types cannot have enumeration members with the same name.

The scope limiting enumeration type introduced by C++11 standard is defined as follows:

enum class Enumeration type name {Enum member 1, enum member 2,..., enum member n};

Compared with the enumeration type without scope, the enumeration members defined by the enumeration type with scope are not accessible outside the scope of the enumeration type.

//Defines an unqualified scope enumeration type
enum color {red, yellow, green};  
//Error, an unrestricted scope enumeration member cannot have the same name as another enumeration type member
enum stoplight{red, yellow, green}; 

//Correctly, the scope of scoped enumeration members is limited to types
enum class newcolor {red, yellow, green}; 
//Correct. Enumeration members can be used in addition to enumeration types that are not scoped
color e_color = green;       

//Error, scoped enumeration members are not accessible outside the type
//although color Type green Members can access, but color And newcolor Different types, non assignable
newcolor ec_color = green;   

In the old standard, enumeration variables can be assigned with integer constants, but in the C++11 standard, in order to initialize or assign values to enumeration objects, an enumeration member of this type or an object of this type must be used. The example code is as follows:

enum color {red, yellow, green};  
enum class newcolor {red, yellow, green};  

color e_var1 = 1;                 //Error, 1 is not an enum type value
color e_var2 = red;                //correct, red yes color Enumeration value of type
color e_var3 = e_var2;             //Correct, assignment between data of the same type

newcolor e_nvar1 = newcolor::red;       //correct, red yes newcolor Enumeration value of type

Enumeration type objects or enumeration members that are not scoped can be automatically converted to integers, so they can be used where integers are needed. Scoped enumeration types do not have this feature. The example code is as follows:

int i = color::red;               //True, scopeless enumeration members can be converted to integers
int j = newcolor::red;             //Error, scoped enumeration members cannot be converted to integers

In addition to adding scope limited enumeration types, the new C++11 standard also improves the original definition form of scope unlimited enumeration types:

1. The type of enumeration member in enumeration type can be specified explicitly by adding colon and enumeration member type after the name of enumeration type.

enum intvalues:unsigned long long{
  chartype = 255, shorttype = 65535, inttype = 4295967295,
  longtype = 4294967295ul,
  longlongtype = 18446744073709551615ul
};

If the enumeration member type is not specified, the scope limited enumeration type member defaults to int, and the scope unlimited enumeration type member does not have a default type. It can be determined that the member type is large enough to accommodate all members. After specifying the enumeration member type, an error will be raised if the value of an enumeration member exceeds the range that the type can accommodate.

2. In C++11, enum can be declared in advance. Enum's pre declaration must specify the enum member type, either implicitly or explicitly.

enum etype1;              //Error, a pre declaration of an unrestricted scope enumeration type should specify a member type
enum etype2:unsigned int;      //correct
enum class etype3;          //Correct, scoped enumeration type members have default types int 
enum class etype4:unsigned int;  //Correct, the pre declaration specifies the enumeration member type

Like all declaration statements, the member types described in the declaration and definition of enumeration must be the same. In addition, you cannot declare an enumeration type without scope and then a qualified scope enumeration type with the same name in the same context.


3. Pre declaration application

enmu class Clolor:char; //Pre declaration enumeration class
void Foo(Color*p); //Use of pre declaration
//....................
enum class Color:char{RED,GREEN,BLACK,WHITE}; //Definition of pre declaration

 

--------

Reference link:

C++11 strong type enumeration -- Enumeration class

Scoped enumeration types - c language learning tutorial_ c language program_ c language programming_ Introduction to c language

Topics: C++