C++ 枚举从 0 开始吗​​?

Do C++ enums Start at 0?

如果我有一个 enum 不为枚举分配数字,它的序数值会是 0 吗?例如:

enum enumeration { ZERO,
                   ONE,
                   TWO,
                   THREE,
                   FOUR,
                   FIVE,
                   SIX,
                   SEVEN,
                   EIGHT,
                   NINE };

我找到了一个 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,我似乎也记得看到过,但我不能说那是多久以前的事了。

我真的很想看到一个为 C++ 证实这一点的答案,但我也想知道序数 0 是否成立 即使 我在enum:

的中间
enum enumeration { ZERO,
                   ONE,
                   TWO,
                   THREE = 13,
                   FOUR,
                   FIVE,
                   SIX,
                   SEVEN,
                   EIGHT,
                   NINE };

根据该标准 [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.

强调我的

所以是的,如果您不指定起始值,它将默认为 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:

这也行。它将从 0 开始并沿途递增。然后在枚举之后,您将值分配给它,它将开始从该值增加一个,用于后续枚举器。

来自第 7.2 节,第 165 页

. If the first enumerator has no initializer, the value of the corresponding constant is zero

来源:http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4527.pdf

来自 C++11 规范 (7.2/2):

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.

所以是的,枚举中的第一个标识符的值为零(如果它没有显式初始化为另一个值),并且每个连续的标识符都将具有前一个标识符的值加一。