什么是c ++枚举的基础类型? [英] What is the underlying type of a c++ 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,
};
哪些是遗产,我必须通过添加几个新值来修改它。
问题是每个值必须是一个唯一的位,所以它们可能被组合到一个位图。
这些值是使用#x ##十六进制格式设置的,但我想知道这是否可以存储的最大值?
如果我将代码更改为
枚举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,
};
从标准C ++ 7.2 / 5:
枚举的基础类型是一个整数类型,可以表示枚举中定义的所有枚举值
。实现定义哪个整数类型用作枚举的基础类型
,除了底层类型不应大于int,除非enu-
merator的值不能适合于int或unsigned int。如果枚举器列表为空,则底层类型为
,就像枚举具有单个枚举值为0的值一样。sizeof()的值应用于enu-
meration类型,枚举对象类型或枚举器是应用于
底层类型的sizeof()的值。
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
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,
};
From Standard 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.
这篇关于什么是c ++枚举的基础类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!