C++ 枚举从 0 开始吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34811486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do C++ enums Start at 0?
提问by Jonathan Mee
If I have an enum
that does not assign numbers to the enumerations, will it's ordinal value be 0? For example:
如果我有一个enum
不为枚举分配数字的值,它的序数值是否为 0?例如:
enum enumeration { ZERO,
ONE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE };
I've been able to find a post citing that the C99 standard requires a 0 ordinal number. But I know C++ ignores several things in the C99 standard. And I've also been able to find a post witnessing the compiler using an ordinal value of 1, something I also seem recall seeing, though I can't say how long ago that was.
我已经能够找到一个帖子,引用 C99 标准需要 0 ordinal number。但我知道 C++ 忽略了 C99 标准中的一些内容。而且我还找到了一篇使用序数值 1 见证编译器的帖子,我似乎也记得看到过,尽管我不能说那是多久以前的事。
I would really like to see an answer that confirms this for C++, but I'd also like to know if an ordinal 0 holds even ifI specify a value in the middle of an enum
:
我真的很想看到一个答案来证实 C++ 的这一点,但我也想知道即使我在 a 中间指定了一个值,序数 0 是否成立enum
:
enum enumeration { ZERO,
ONE,
TWO,
THREE = 13,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE };
回答by NathanOliver
Per that standard [dcl.enum]
根据该标准 [dcl.enum]
The enumeration type declared with an enum-key of only enum is an unscoped enumeration, and its enumerators are unscoped enumerators. The enum-keys enum class and enum struct are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators. The optional identifier shall not be omitted in the declaration of a scoped enumeration. The type-specifier-seq of an enum-base shall name an integral type; any cv-qualification is ignored. An opaqueenum-declaration declaring an unscoped enumeration shall not omit the enum-base. The identifiers in an enumerator-list are declared as constants, and can appear wherever constants are required. An enumeratordefinition with = gives the associated enumerator the value indicated by the constant-expression. If the first enumerator has no initializer, the value of the corresponding constant is zero.An enumerator-definition without an initializer gives the enumerator the value obtained by increasing the value of the previous enumerator by one.
仅使用 enum-key 声明的枚举类型是无作用域枚举,其枚举器是无作用域枚举器。enum-keys enum class 和 enum struct 在语义上是等价的;使用其中之一声明的枚举类型是范围枚举,其枚举数是范围枚举数。在范围枚举的声明中不应省略可选标识符。enum-base 的 type-specifier-seq 应命名一个整型;忽略任何 cv 限定。声明无作用域枚举的 opaqueenum-declaration 不应省略 enum-base。枚举器列表中的标识符被声明为常量,并且可以出现在需要常量的任何地方。带有 = 的枚举器定义为关联的枚举器提供常量表达式指示的值。如果第一个枚举器没有初始化器,则相应常量的值为零。没有初始化器的枚举器定义通过将前一个枚举器的值增加 1 来为枚举器提供值。
Emphasis mine
强调我的
So yes if you do not specify a start value it will default to 0.
所以是的,如果您不指定起始值,它将默认为 0。
I would really like to see an answer that confirms this for C++, but I'd also like to know if an ordinal 0 holds even if I specify a value in the middle of an enum:
我真的很想看到一个确认 C++ 的答案,但我也想知道即使我在枚举中间指定了一个值,序数 0 是否成立:
This also works. It will start at 0 and increment up along the way. Then after the enum you assign the value to it will begin to increase from that value for the next ones.
这也有效。它将从 0 开始并沿途递增。然后在枚举之后,您为其分配值将开始从该值开始增加下一个值。
回答by Some programmer dude
From the C++11 specification (7.2/2):
来自 C++11 规范 (7.2/2):
If the first enumeratorhas no initializer, the value of the corresponding constant is zero. An enumerator-definitionwithout an initializergives the enumeratorthe value obtained by increasing the value of the previous enumeratorby one.
如果第一个枚举器没有initializer,则相应常量的值为零。一个枚举清晰度而没有初始化给出枚举通过增加前的值而获得的值枚举由一个。
So yes, the first identifier in the enumeration will have the value zero (if it's not explicitly initialized to another value), and each consecutive identifier will have the value of the previous plus one.
所以是的,枚举中的第一个标识符的值将为零(如果它没有显式初始化为另一个值),并且每个连续的标识符都将具有前一个的值加一。
回答by Richard Hodges
from § 7.2, p 165
来自第 7.2 节,第 165 页
. If the first enumerator has no initializer, the value of the corresponding constant is zero
. 如果第一个枚举器没有初始化器,则对应常量的值为零
source: http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4527.pdf
来源:http: //open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4527.pdf