本文介绍了值初始化枚举的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 首先,我想说,根据cppreference.com,有点不可能重新初始化枚举。 根据 http://en.cppreference.com/w/cpp/language/value_initialization ,值初始化枚举实际上执行零初始化。然后,根据 http://en.cppreference.com/w/cpp/语言/零初始化,零初始化枚举的效果是:但是,积分常量零并不能隐式转换为枚举。最终,枚举不能被初始化。这听起来很奇怪,价值初始化一个枚举确实在VC,GCC和cl声上起作用。那么这个标准是什么呢? 其次,根据 http://en.cppreference.com/w/cpp/language/static_cast :那么这是否意味着值初始化一个枚举(如果它有效如果目标枚举没有枚举器等于 0 ?解决方案在评论中给出了答案。我试图解释其背后的整个标准对象。(枚举是标量类型; §3.9/ 9)因为转换不是隐含的,我们不在§4中,而是§5.2.9; 必须显示为零在所有枚举的枚举值的范围内。 接下来的五个引号来自§7.2/ 8:由于所有允许的底层类型在其值*的范围内都包含零,所以会自动给出所需的结果。现在,对于没有固定底层类型的枚举, | e max | 是非负数,两个数字的最大值至少与两个数字一样大。因此, max(| e | - K,| e |)也是非负的,而 max 必须大于或等于该数字 - 所以我们的第一个要求才符合。 b 显然为零或否定:如上所示,最小 是非负数,并且 K 是非负数(0或1),因此它们的和的加性逆是非正的。我们的第二个要求得到满足最后,通过设置 e = e = 0 ,可以获得上述结果。 这减少到声明所有积分类型在其值范围内都为零,留给读者证明。 li> First, I want to say, according to cppreference.com, it is somewhat impossible to value-initialize an enum.According to http://en.cppreference.com/w/cpp/language/value_initialization, value-initializing an enum actually performs zero-initialization. It then follows that, according to http://en.cppreference.com/w/cpp/language/zero_initialization, the effect of zero-initializing an enum is:However, an integral constant zero is not implicitly convertible to an enum. Ultimately, an enum cannot be value-initialized. This sounds weird, and value-initializing an enum does work on VC, GCC, and clang. So, what does the standard say about this?Second, according to http://en.cppreference.com/w/cpp/language/static_cast:So, does this imply that value-initializing an enum (if it works at all) may actually lead to undefined behavior if the target enum does not have an enumerator equal to 0? 解决方案 The answer to this was given in the comments. My attempt of explaining the entire standardese behind it is given below.(Enumerations are scalar types; §3.9/9)So as the conversion is not said to be implicit, we're not looking in §4, but §5.2.9; §5.2.9/10 then defines how integral values are converted to enumeration types.It must be shown that zero is in the range of enumeration values for all enumerations.The next five quotes are taken from §7.2/8:Since all permitted underlying types include zero in their range of values*, this automatically gives the desired result. Now, for enumerations without fixed underlying types, I.e. we have to show that b is always less than or equal to zero, and b is always greater or equal to zero.|e | is non-negative, and the maximum of two numbers is at least as large as both numbers are. Hence max(|e | − K, |e |) is non-negative as well, and b must be greater or equal to that number - so our first requirement is met.b is clearly either zero or negative: b is non-negative as shown above, and K is non-negative (0 or 1), hence the additive inverse of their sum is non-positive. Our second requirement is met. Finally,This leads to the above result by setting e = e = 0.This reduces to the claim "All integral types have zero in their range of values", which is left to prove for the reader. 这篇关于值初始化枚举的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 16:50