按照芭丝谢芭的要求,并作为一个后续问题"如果一个枚举不适合整体类型会怎样?" :
假设枚举定义如下:
enum foo : unsigned int
{
bar = UINT_MAX,
oops
};
Run Code Online (Sandbox Code Playgroud)
是oops定义的价值还是不是?
MSVS2015编译:
warning C4340: 'oops': value wrapped from positive to negative value
warning C4309: 'initializing': truncation of constant value
warning C4369: 'oops': enumerator value '4294967296' cannot be represented as 'unsigned int', value is '0'
Run Code Online (Sandbox Code Playgroud)
MSVS2015输出:
bar = 4294967295
oops= 0
Run Code Online (Sandbox Code Playgroud)
gcc 4.9.2编译:
9 : note: in expansion of macro 'UINT_MAX'
bar = UINT_MAX,
^
10 : error: enumerator value 4294967296l is …Run Code Online (Sandbox Code Playgroud) 这可能已在其他地方得到解答,但我找不到合适的答案.
我有这个代码:
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,
};
Run Code Online (Sandbox Code Playgroud)
哪个是遗产,我必须通过添加一些新值来修改它.问题是每个值必须是唯一的位,因此它们可以组合成位图.
这些值是使用#x ##十六进制格式设置的,但我想知道这是否是它可以存储的最大值?如果我将代码更改为,会产生什么影响(如果有的话)
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,
};
Run Code Online (Sandbox Code Playgroud)