简体   繁体   English

C ++双枚举值分配

[英]C++ doubly enum value assignment

In an open source code I found an enum initialization that looked like that: 在一个开放源代码中,我发现了一个枚举初始化,如下所示:

enum example {
    FIRST,
    FIRST_AGAIN = FIRST,
    SECOND,
    ETC
};

I was surprised how the values of the enum are going to be initialized, because the FIRST_AGAIN would get the value 0 because FIRST is 0 , too. 我很惊讶枚举的值将如何初始化,因为FIRST_AGAIN也会因为FIRST也为0而得到值0 But i thought it could affect the FIRST value to be -1 , when the FIRST_AGAIN value is 0 . 但是我认为当FIRST_AGAIN值为0时,它可能会影响FIRST值为-1

So I looked with the debugger what values are assigned to the key words. 因此,我与调试器一起查看了为关键字分配了哪些值。 FIRST and FIRST_AGAIN are assigned with 0 . FIRSTFIRST_AGAIN分配了0

So, my question is, is it really allowed (GCC compiles it, but what does the C++ standard say?) to assign two key words in an enumaration with the same value? 因此,我的问题是,是否真的允许(GCC编译它,但是C ++标准怎么说?)在枚举中分配两个具有相同值的关键字? And how does the compiler translate the assignment? 编译器如何翻译任务? Because to know the value of FIRST , the compiler should gone through the whole enum first and look if any key word is assigned, back from there, he could assign all the other values, but he doesn't know what the assignment FIRST_AGAIN = FIRST means. 因为要知道FIRST的值,所以编译器应该首先遍历整个枚举,然后查看是否分配了任何关键字,从那里可以分配所有其他值,但是他不知道FIRST_AGAIN = FIRST的分配是什么手段。

Maybe the question is stupid and it's common use to do something like that. 也许这个问题很愚蠢,通常可以这样做。 What would be the benefits of assigning two key words with the same value? 分配两个具有相同值的关键字有什么好处?

So, my question is, is it really allowed (GCC compiles it, but what does the C++ standard say?) to assign two key words in an enumaration with the same value? 因此,我的问题是,是否真的允许(GCC编译它,但是C ++标准怎么说?)在枚举中分配两个具有相同值的关键字?

That's fine. 没关系。 The C++ standard doesn't say anything about it in the normative text, so it's implicitly allowed, but does include an example: C ++标准在规范文本中未提及任何内容,因此它是隐式允许的,但包含示例:

 enum { a, b, c=0 }; 

Which defines a and c as zero. ac定义为零。

And how does the compiler translate the assignment? 编译器如何翻译任务? Because to know the value of FIRST , the compiler should gone through the whole enum first 因为要知道FIRST的值,所以编译器应该首先遍历整个枚举

No, the compiler doesn't need to do that. 不,编译器不需要这样做。 An enumerator definition without an explicit value doesn't depend on later enumerator definitions. 没有显式值的枚举器定义不依赖于更高版本的枚举器定义。 Its value is the value of the previous enumerator plus one, or zero if there were no previous enumerators. 它的值是前一个枚举数的值加1,如果没有以前的枚举数,则为零。

Maybe the question is stupid and it's common use to do something like that. 也许这个问题很愚蠢,通常可以这样做。 What would be the benefits of assigning two key words with the same value? 分配两个具有相同值的关键字有什么好处?

One possibility: 一种可能性:

enum E {
  A = 20,
  B = 30,
  C = 40,
  MIN = A,
  MAX = C
}

Here, it may be expected that the values of MIN and MAX may change as enumerators are added or removed, but the values of A , B and C are expected to remain constant. 在这里,可以预期的是, MINMAX的值可能会随着添加或删除枚举数而改变,但是ABC的值预计将保持不变。 By using C where C is wanted, and MIN and MAX where a range is wanted, code may be future-proof for later versions of whatever library defines this enumeration. 通过使用C其中C被通缉,而MINMAX享受一系列被通缉,代码可能是面向未来的更高版本的任何的库定义此枚举。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM