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

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

问题描述

如果我有一个没有为枚举分配数字的枚举,它的序数值是否为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]


枚举类型只用枚举枚举枚举是一个未定义的枚举,其枚举器是不带范围的枚举器。枚举键枚举类和枚举结构在语义上相当;使用其中一个声明的枚举类型是范围枚举,其枚举器的作用域为
枚举器。可选标识符在范围枚举的声明中不得省略。枚举类型的说明符-seq应命名为一个整数类型;任何cv资格被忽略。声明枚举的opaqueenum声明不能省略枚举。枚举器列表中的标识符被声明为常量,并且可以出现在需要常量的地方。 enumeratordefinition with =给出关联的枚举器由constant-expression指示的值。 如果第一个
枚举器没有初始化器,则相应常量的值为零。
没有初始化器的枚举器定义为枚举器提供了通过将先前枚举器的值增加获得的值一个。

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开始,一路上升。然后在枚举之后,您将赋值给它将从下一个值的值开始增加。

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.

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

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