• <legend id='p2eBQ'><style id='p2eBQ'><dir id='p2eBQ'><q id='p2eBQ'></q></dir></style></legend>

      <i id='p2eBQ'><tr id='p2eBQ'><dt id='p2eBQ'><q id='p2eBQ'><span id='p2eBQ'><b id='p2eBQ'><form id='p2eBQ'><ins id='p2eBQ'></ins><ul id='p2eBQ'></ul><sub id='p2eBQ'></sub></form><legend id='p2eBQ'></legend><bdo id='p2eBQ'><pre id='p2eBQ'><center id='p2eBQ'></center></pre></bdo></b><th id='p2eBQ'></th></span></q></dt></tr></i><div id='p2eBQ'><tfoot id='p2eBQ'></tfoot><dl id='p2eBQ'><fieldset id='p2eBQ'></fieldset></dl></div>
        <bdo id='p2eBQ'></bdo><ul id='p2eBQ'></ul>
    1. <tfoot id='p2eBQ'></tfoot>

      <small id='p2eBQ'></small><noframes id='p2eBQ'>

        是否允许枚举具有未列出的值?

        Is it allowed for an enum to have an unlisted value?(是否允许枚举具有未列出的值?)

            <tbody id='fwwvS'></tbody>
        • <legend id='fwwvS'><style id='fwwvS'><dir id='fwwvS'><q id='fwwvS'></q></dir></style></legend>
        • <small id='fwwvS'></small><noframes id='fwwvS'>

            <bdo id='fwwvS'></bdo><ul id='fwwvS'></ul>
                <i id='fwwvS'><tr id='fwwvS'><dt id='fwwvS'><q id='fwwvS'><span id='fwwvS'><b id='fwwvS'><form id='fwwvS'><ins id='fwwvS'></ins><ul id='fwwvS'></ul><sub id='fwwvS'></sub></form><legend id='fwwvS'></legend><bdo id='fwwvS'><pre id='fwwvS'><center id='fwwvS'></center></pre></bdo></b><th id='fwwvS'></th></span></q></dt></tr></i><div id='fwwvS'><tfoot id='fwwvS'></tfoot><dl id='fwwvS'><fieldset id='fwwvS'></fieldset></dl></div>
              • <tfoot id='fwwvS'></tfoot>

                1. 本文介绍了是否允许枚举具有未列出的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  说,我们有

                  enum E
                  {
                    Foo = 0,
                    Bar = 1
                  };
                  

                  现在,我们做

                  enum E v = ( enum E ) 2;
                  

                  然后

                  switch ( v )
                  {
                    case Foo:
                      doFoo();
                    break;
                    case Bar:
                      doBar();
                    break;
                    default:
                      // Is the compiler required to honor this?
                      doOther();
                    break;
                  }
                  

                  由于上面的开关处理枚举的每个可能列出的值,是否允许编译器优化掉上面的 default 分支,或者在值的情况下具有未指定或未定义的行为枚举不在列表中?

                  Since the switch above handles every possible listed value of the enum, is it allowed for the compiler to optimize away the default branch above, or otherwise have an unspecified or undefined behavior in the case the value of enum is not in the list?

                  因为我期望 C 和 C++ 的行为应该相似,所以问题是关于这两种语言的.但是,如果 C 和 C++ 之间在这种情况下存在差异,那么了解它也会很高兴.

                  As I am expecting that the behavior should be similar for C and C++, the question is about both languages. However, if there's a difference between C and C++ for that case, it would be nice to know about it, too.

                  推荐答案

                  C++ 情况

                  在 C++ 中,每个枚举都有一个底层的整数类型.如果明确指定(例如:enum test2 : long { a,b};)或int,则可以固定默认在 scoped 枚举的情况下(例如:enum class test { a,b };):

                  C++ situation

                  In C++, each enum has an underlying integral type. It can be fixed, if it is explicitly specified (ex: enum test2 : long { a,b};) or if it is int by default in the case of a scoped enum (ex: enum class test { a,b };):

                  [dcl.enum]/5: 每个枚举定义了一个不同于所有其他类型的类型.每个枚举还有一个底层类型.(...) 如果不显式指定的范围枚举类型的基础类型是整数.在这些情况下,底层类型被认为是固定的.

                  [dcl.enum]/5: Each enumeration defines a type that is different from all other types. Each enumeration also has an underlying type. (...) if not explicitly specified, the underlying type of a scoped enumeration type is int. In these cases, the underlying type is said to be fixed.

                  对于基础类型未明确固定的无作用域枚举(您的示例),该标准为您的编译器提供了更大的灵活性:

                  In the case of an unscoped enum where the underlying type was not explicitely fixed (your example), the standard gives more flexibility to your compiler:

                  [dcl.enum]/7: 对于底层类型不固定的枚举,底层类型是一个整数类型,可以表示所有的枚举中定义的枚举值.(...) 使用哪种整数类型作为底层是实现定义的type 除了基础类型不能大于 int除非枚举器的值不能放入 int 或 unsigned内部

                  [dcl.enum]/7: For an enumeration whose underlying type is not fixed, the underlying type is an integral type that can represent all the enumerator values defined in the enumeration. (...) It is implementation-defined which integral type is used as the underlying type except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int.

                  现在非常棘手的事情:枚举变量可以保存的值取决于基础类型是否固定:

                  Now a very tricky thing: the values that can be held by an enum variable depends on whether or not the underlying type is fixed:

                  • 如果它是固定的,"枚举的值就是基础类型.

                  否则,就是能容纳最小枚举数和最大枚举数的最小位域的最小值和最大值内的整数值.

                  otherwhise, it is the integral values within the minimum and the maximum of the smallest bit-field that can hold the smallest enumerator and the largest one.

                  您处于第二种情况,虽然您的代码可以在大多数编译器上运行,但最小的位域的大小为 1,因此您可以确定所有兼容 C++ 编译器的唯一值是 0 和 1 之间的值...

                  You are in the second case, although your code will work on most compilers, the smalest bitfield has a size of 1 and so the only values that you can for sure hold on all compliant C++ compilers are those between 0 and 1...

                  结论:如果要确保该值可以设置为 2,则必须使枚举成为作用域枚举,或者明确指示基础类型.**

                  Conclusion: If you want to ensure that the value can be set to 2, you either have to make your enum a scoped enum, or explicitly indicate an underlying type.**

                  更多阅读:

                  • 关于如何检查枚举值是否有效的问题
                  • 关于在安全编码中避免枚举超出范围的文章.
                  • Stroutstrup 的 plaidoyer 用于作用域枚举而不是非作用域枚举

                  C 的情况要简单得多(C11):

                  The C situation is much simpler (C11):

                  6.2.5/16: 枚举包含一组命名的整数常量值.每个不同的枚举构成一个不同的枚举类型.

                  6.2.5/16: An enumeration comprises a set of named integer constant values. Each distinct enumeration constitutes a different enumerated type.

                  所以基本上,它是一个整数:

                  So basically, it is an int:

                  6.7.2.2./2 定义枚举常量值的表达式应为具有值的整数常量表达式可表示为 int.

                  6.7.2.2./2 The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.

                  有以下限制:

                  每个枚举类型应与char兼容,一个有符号整数类型,或无符号整数类型.类型的选择是实现定义,但应能够表示枚举所有成员的值.

                  Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration.

                  这篇关于是否允许枚举具有未列出的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)
                  <tfoot id='c0oLC'></tfoot>
                2. <i id='c0oLC'><tr id='c0oLC'><dt id='c0oLC'><q id='c0oLC'><span id='c0oLC'><b id='c0oLC'><form id='c0oLC'><ins id='c0oLC'></ins><ul id='c0oLC'></ul><sub id='c0oLC'></sub></form><legend id='c0oLC'></legend><bdo id='c0oLC'><pre id='c0oLC'><center id='c0oLC'></center></pre></bdo></b><th id='c0oLC'></th></span></q></dt></tr></i><div id='c0oLC'><tfoot id='c0oLC'></tfoot><dl id='c0oLC'><fieldset id='c0oLC'></fieldset></dl></div>
                    <bdo id='c0oLC'></bdo><ul id='c0oLC'></ul>
                  • <legend id='c0oLC'><style id='c0oLC'><dir id='c0oLC'><q id='c0oLC'></q></dir></style></legend>

                          <tbody id='c0oLC'></tbody>

                        <small id='c0oLC'></small><noframes id='c0oLC'>