Nature of the enum type

Source: Internet
Author: User

Since the C language, The enum type has been introduced into the language as a constant of a finite set of user-defined classes.
And once became the only method for defining constants in the compilation period in C ++ (a static integer constant was introduced in the class ).
Based on the description of the enum type, what type is defined by enum? As
What is the memory space occupied by a user-defined type? Whether the enum type can be used is actually limited
What is the boundary constraint of a set constant? We may all know that The enum and int types have implicit (automatic) conversion rules,
Is it true that enum variables can be used anywhere to replace int variables? The following will be done one by one
Answer these questions.
1. What type is defined by enum?
In C ++, we all know that there are only two types of classification: POD type and class type (unknown parameters are acceptable ).
See my other articles ). The type defined by enum actually belongs to the POD type, that is, it will participate in the POD
The implicit conversion between the enum type and the int type occurs only when the implicit conversion rules are removed.
That is to say, the type defined by enum does not have the namespace limitation capability (because it does not belong to the class type ),
The defined constant quantum has the same visibility as the namespace Of The enum type.
Limitation, so name conflicts may occur. For example:

struct CEType
{
enum EType1 { e1, e2 };
enum EType2 { e1, e2 };
};

In the preceding example, an error occurs when the names of e1 and e2 conflict with each other. The reason is that the enumeration sub (e1 and e2) is
The name in the CEType namespace must also use CEType: e1 to reference the enumeration in the CEType.
This method is used instead of CEType: EType1: e1 for reference.
2. What is the memory space occupied by a user-defined type?
This is the question about the number of sizeof (EType1) equals, is it a user-defined enumeration class?
Are all of the same sizes? An enumeration class in most 32-bit compilers (such as VC ++ and gcc)
The size of the type is actually the size of a sizeof (int). Is it true that the size of the enumeration type is int?
Type size? This is not the case. It is not defined in the C ++ standard document,
In the standard, it is described as follows: "The size of the enumeration type is an integer that can hold the maximum enumerated sub-values ",
In addition, the standard also says: "The enumerated sub-values in the enumeration type must be expressed in an int type ",
That is to say, the size of the enumeration type cannot exceed the size of the int type, but it must be the same as that of the int type.
What about the same size? The above standard has been clearly stated, as long as it can accommodate the largest enumerated sub-
The integer of the value can be char, short, and int. For example:

enum EType1 { e1 = CHAR_MAX };
enum EType2 { e2 = SHRT_MAX };
enum EType3 { e3 = INT_MAX };

The preceding three enumeration types can be expressed in the memory space of char, short, and int respectively, that is:

sizeof( EType1 ) == sizeof( char  );
sizeof( EType2 ) == sizeof( short );
sizeof( EType3 ) == sizeof( int );

So Why will the above three enumeration types be compiled into the int size in a 32-bit compiler?
The requirements for the 32-bit data memory are mainly considered. In some computer hardware environments
Mandatory requirements (such as sun iSCSI), some of which are processed by a complete 32-bit font-length CPU.
The reason for high efficiency (for example, IA32 ). Therefore, we cannot simply assume that the size of the enumeration type is int type.
Type, you may encounter a compiler using the above processing strategy to save memory.
3. Can I use the enum type to constrain the boundary of Finite Set constants?
First, let's take a look at the following example:

enum EType { e1 = 0, e2 };
void func1( EType e )
{
if ( e == e1 )
{
// do something
}
// do something because e != e1 must e == e2
}
void func2( EType e )
{
if ( e == e1 )
{
// do something
}
else if ( e == e2 )
{
// do something
}
}

func1( static_cast<EType>( 2 ) );
func2( static_cast<EType>( -1 ) );

The above code should clearly describe such an exception.
When the type value calls the func1 function, it will cause the function to take the action that should not be taken, and the second function may be better.
It only ignores the value out of the range. This indicates that the enumerated type is not really a strong type.
And declare the two function parameters as integers
Any difference. Therefore, pay attention to the enumeration type traps in the standard definition. (In fact, only the class type is true.
Positive strong type)

4. Is it true that enum variables can be used anywhere to replace int variables?
As discussed above, the enumerated variables and integer variables have too much consistency and interchangeable,
Is it possible to replace the int type with the enumeration type in every place where the int type can be used?
In fact, this is not the case. After all, the enumeration type is a type that can be distinguished during compilation. At the same time, it is analyzed at 2nd points.
Different enumeration types and int types have the same size, which determines that in some cases it is not
Replace the int type with the enumerated type. For example:
First case:

enum EType { e1 = 0, e2, e3 };
EType val;
std::cin >> val;

Case 2:

enum EType { e1 = 0, e2, e3 };
EType val;
std::scanf( "%d", &val );

The two cases above are basically the same type of problems, but they are not. The first case causes
An error occurs during compilation because std: cin does not define the corresponding Enumeration type overload> operator.
It indicates that the enumeration type is an independent and authenticated type. In the second case, there will be no compilation problems,
However, the scanf function Stack may be damaged and the program running is invalid. Why? Above
We have analyzed that the dimensions of the enumerated type variables are not the same as those of the int type, so we use % d
It means to treat the enumerated type variable val as a 4-byte int variable and press the parameter stack.
The sizeof (val) in the interpreter is equal to 1 byte, so that the scanf function will
The Byte address is also pushed into the stack and assigned values. Maybe the subsequent three byte addresses of the val variable do not
The special meaning can be rewritten (for example, the byte-aligned empty address space), and it may be considered that there will be no error
Otherwise, the scanf function will be cleaned up in the stack after it is called.
Excessive address space is cleared, which destroys the pointer to the stack of the peripheral function.
An error occurred while running in sequence.

Http://www.cppblog.com/chemz/archive/2007/06/05/25578.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.