C++ 枚举从 0 开始吗​​? [英] Do C++ enums Start at 0?

查看:48
本文介绍了C++ 枚举从 0 开始吗​​?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have an enum that does not assign numbers to the enumerations, will it's ordinal value be 0? For example:

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

我找到了一篇帖子,其中提到 C99 标准需要 0 序数.但我知道 C++ 忽略了 C99 标准中的几件事.而且我还找到了使用序数值 1 见证编译器的帖子,我似乎也记得看,虽然我不能说那是多久以前.

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.

我真的很想看到一个答案来证实 C++ 的这一点,但我也想知道序数 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:

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

推荐答案

按照那个标准 [dcl.enum]

Per that standard [dcl.enum]

仅使用 enum-key 声明的枚举类型是无作用域枚举,其枚举器是无作用域枚举器.enum-keys enum class 和 enum struct 在语义上是等价的;使用其中之一声明的枚举类型是范围枚举,其枚举数是范围的枚举器.在范围枚举的声明中不应省略可选标识符.enum-base 的 type-specifier-seq 应命名一个整型;忽略任何 cv 限定.声明无作用域枚举的 opaqueenum-declaration 不应省略 enum-base.枚举器列表中的标识符被声明为常量,并且可以出现在需要常量的任何地方.带有 = 的枚举器定义为关联的枚举器提供常量表达式指示的值.如果第一个enumerator 没有初始化器,对应的常量的值为零. 没有初始化器的 enumerator-definition 给 enumerator 赋予前一个 enumerator 的值加 1 得到的值.

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.

So yes, if you do not specify a start value, it will default to 0.

我真的很想看到一个可以证实 C++ 的答案,但我也想知道即使我在枚举中间指定了一个值,序数 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 开始并沿途递增.然后在枚举之后,您将值分配给它,随后的枚举器将从该值开始增加 1.

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 by one from that value for subsequent enumerator.

这篇关于C++ 枚举从 0 开始吗​​?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆