This may have been answered elsewhere but I could not find a suitable response.
这个问题可能在其他地方得到了回答,但我找不到合适的回答。
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,
};
Which is legacy and I have to modify it by adding a few new values. The issue is each value must be a unique bit so they may be OR combined to a bitmap.
这是遗留问题,我必须通过添加一些新值来修改它。问题是每个值都必须是唯一的位,所以它们可以是或合并到位图。
The values are set using the #x## hex format, but I'm wondering if this is the max it can store? What will be the effect, if any, if I change my code to
这些值是使用#x## hex格式设置的,但是我想知道这是否是它可以存储的最大值?如果我将代码更改为,会有什么影响
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,
};
4 个解决方案
#1
36
From Standard C++ 7.2/5:
从标准c++ 7.2/5:
The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration. It is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enu- merator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0. The value of sizeof() applied to an enu- meration type, an object of enumeration type, or an enumerator, is the value of sizeof() applied to the underlying type.
枚举的底层类型是一个完整类型,它可以表示枚举中定义的所有枚举数值。是实现定义整型作为底层为枚举类型,除了底层不得大于int类型除非enu表示的价值——merator不能适应int或unsigned int。如果enumerator-list是空的,底层的类型是枚举仿佛一个枚举器,值0。应用于enu- meration类型、枚举类型对象或枚举器的sizeof()的值是应用于底层类型的sizeof()的值。
#2
28
The type of a C++ enum is the enum itself. Its range is rather arbitrary, but in practical terms, its underlying type is an int
.
c++枚举类型是enum本身。它的范围相当随意,但实际上,它的底层类型是int类型。
It is implicitly cast to int
wherever it's used, though.
不过,无论在哪里使用它,它都被隐式地转换为int。
C++11 changes
This has changed since C++11, which introduced typed enums. An untyped enum
now is defined as being at least the width of int
(and wider if larger values are needed). However, given a typed enum
defined as follows:
自从c++ 11引入了类型枚举之后,这种情况已经发生了变化。未类型化的枚举现在被定义为至少是int的宽度(如果需要更大的值,则更宽)。但是,给定一个类型的enum,定义如下:
enum name : type {};
An enumeration of type name
has an underlying type of type
. For example, enum : char
defines an enum
the same width as char
instead of int
.
类型名称的枚举类型有一个底层类型。例如,enum: char定义了与char而不是int相同的宽度。
Further, an enum
can be explicitly scoped as follows:
此外,枚举可以显式地限定如下:
enum class name : type {
value = 0,
// ...
};
(Where name
is required, but type
is optional.) An enum
declared this way will no longer implicitly cast to its underlying type (requiring a static_cast<>
) and values must be referenced with a fully-qualified name. In this example, to assign value
to a enum
variable, you must refer to it as name::value
.
(需要名称,但类型是可选的)以这种方式声明的enum将不再隐式地转换到它的底层类型(需要static_cast<>),并且必须使用完全限定的名称引用值。在本例中,要为enum变量赋值,必须将其引用为name::value。
#3
1
IIRC its represented as int in memory. But gcc has switch -fshort-enum
to make it a shortest integer type that fits all the values, if you need to save space. Other compilers will have something similar.
IIRC在内存中表示为int。但是gcc有开关-fshort-enum,如果您需要节省空间,可以将它设置为适合所有值的最短整数类型。其他编译器也会有类似的情况。
#4
-1
The underlying type is the smallest signed integer fitting the biggest/smallest value of your enum.
底层类型是最小的带符号整数,适合枚举的最大/最小值。
#1
36
From Standard C++ 7.2/5:
从标准c++ 7.2/5:
The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration. It is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enu- merator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0. The value of sizeof() applied to an enu- meration type, an object of enumeration type, or an enumerator, is the value of sizeof() applied to the underlying type.
枚举的底层类型是一个完整类型,它可以表示枚举中定义的所有枚举数值。是实现定义整型作为底层为枚举类型,除了底层不得大于int类型除非enu表示的价值——merator不能适应int或unsigned int。如果enumerator-list是空的,底层的类型是枚举仿佛一个枚举器,值0。应用于enu- meration类型、枚举类型对象或枚举器的sizeof()的值是应用于底层类型的sizeof()的值。
#2
28
The type of a C++ enum is the enum itself. Its range is rather arbitrary, but in practical terms, its underlying type is an int
.
c++枚举类型是enum本身。它的范围相当随意,但实际上,它的底层类型是int类型。
It is implicitly cast to int
wherever it's used, though.
不过,无论在哪里使用它,它都被隐式地转换为int。
C++11 changes
This has changed since C++11, which introduced typed enums. An untyped enum
now is defined as being at least the width of int
(and wider if larger values are needed). However, given a typed enum
defined as follows:
自从c++ 11引入了类型枚举之后,这种情况已经发生了变化。未类型化的枚举现在被定义为至少是int的宽度(如果需要更大的值,则更宽)。但是,给定一个类型的enum,定义如下:
enum name : type {};
An enumeration of type name
has an underlying type of type
. For example, enum : char
defines an enum
the same width as char
instead of int
.
类型名称的枚举类型有一个底层类型。例如,enum: char定义了与char而不是int相同的宽度。
Further, an enum
can be explicitly scoped as follows:
此外,枚举可以显式地限定如下:
enum class name : type {
value = 0,
// ...
};
(Where name
is required, but type
is optional.) An enum
declared this way will no longer implicitly cast to its underlying type (requiring a static_cast<>
) and values must be referenced with a fully-qualified name. In this example, to assign value
to a enum
variable, you must refer to it as name::value
.
(需要名称,但类型是可选的)以这种方式声明的enum将不再隐式地转换到它的底层类型(需要static_cast<>),并且必须使用完全限定的名称引用值。在本例中,要为enum变量赋值,必须将其引用为name::value。
#3
1
IIRC its represented as int in memory. But gcc has switch -fshort-enum
to make it a shortest integer type that fits all the values, if you need to save space. Other compilers will have something similar.
IIRC在内存中表示为int。但是gcc有开关-fshort-enum,如果您需要节省空间,可以将它设置为适合所有值的最短整数类型。其他编译器也会有类似的情况。
#4
-1
The underlying type is the smallest signed integer fitting the biggest/smallest value of your enum.
底层类型是最小的带符号整数,适合枚举的最大/最小值。