What is the basic type of C ++ enumeration?

This could be answered elsewhere, but I could not find a suitable answer.

I have this code:

enum enumWizardPage { WP_NONE = 0x00, WP_CMDID = 0x01, WP_LEAGUES = 0x02, WP_TEAMS = 0x04, WP_COMP = 0x08, WP_DIVISIONS = 0x10, WP_FORMULAS = 0x20, WP_FINISHED = 0x40, }; 

This is a legacy, and I have to change it by adding a few new values. The problem is that each value must be a unique bit, so they can be combined with a bitmap.

Values ​​are set using the format # x ## hex, but I wonder if this can be kept to a maximum? What will be the effect, if any, if I change my code to

 enum enumWizardPage { WP_NONE = 0x0000, WP_CMDID = 0x0001, WP_LEAGUES = 0x0002, WP_TEAMS = 0x0004, WP_COMP = 0x0008, WP_DIVISIONS = 0x0010, WP_FORMULAS = 0x0020, WP_FINISHED = 0x0040, }; 
+35
c ++ enums types size hex
Jul 13. '09 at 21:13
source share
4 answers

From N4659 C ++ 7.2 / 5 :

For an enumeration whose base type is not fixed, the base type is an integer type that can represent all the values ​​of the enumerator defined in the enumeration. If no integer type can represent all the values ​​of the enumerator, the enumeration is incorrectly formed. It is determined by the implementation which integral type is used as the base type, except that the base type must not be larger than int unless the value of the enumerator can fit in int or unsigned int . If the enumerator list is empty, the base type is as if the enumeration had a single enumerator with a value of 0.

+38
Jul 13 '09 at 21:18
source share

The C ++ enumeration type is an enumeration. Its range is quite arbitrary, but from a practical point of view, its basic type is int .

It is implicitly added to the int , wherever it is used.

Changes in C ++ 11

This has changed from C ++ 11, which introduced typed enumerations. An untyped enum is now defined as having at least int width (and wider if larger values ​​are needed). However, given a typed enum defined as follows:

 enum name : type {}; 

An enumeration of type name has a base type of type . For example, enum : char defines enum the same width as char instead of int .

In addition, a enum can be explicitly covered as follows:

 enum class name : type { value = 0, // ... }; 

(Where name is required, but type is optional.) An enum declared in this way will no longer implicitly discard its base type (requiring static_cast<> ), and the values ​​must be specified with a fully-qualified name. In this example, to assign value to enum you must reference it as name::value .

+40
Jul 13 '09 at 21:15
source share

IIRC it is represented as an int in memory. But gcc has the -fshort-enum switch to make it the shortest integer type that matches all values ​​if you need to save space. Other compilers will have something similar.

+1
Jul 13 '09 at 21:19
source share

The main type is the smallest signed integer corresponding to the largest / smallest value of your enumeration.

-one
Jul 13 '09 at 21:21
source share



All Articles