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

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

        检测多个枚举项何时映射到相同的值

        Detect when multiple enum items map to same value(检测多个枚举项何时映射到相同的值)
        • <small id='UejYO'></small><noframes id='UejYO'>

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

              1. <legend id='UejYO'><style id='UejYO'><dir id='UejYO'><q id='UejYO'></q></dir></style></legend>

                  本文介绍了检测多个枚举项何时映射到相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否有一种编译时方法来检测/防止 C/C++ 枚举中的重复值?

                  Is there a compile-time way to detect / prevent duplicate values within a C/C++ enumeration?

                  问题在于有多个项目被初始化为显式值.

                  背景:

                  我继承了一些 C 代码,例如:

                  I've inherited some C code such as the following:

                  #define BASE1_VAL    (5)
                  #define BASE2_VAL    (7)
                  
                  typedef enum
                  {
                    MsgFoo1A = BASE1_VAL,       // 5
                    MsgFoo1B,                   // 6
                    MsgFoo1C,                   // 7
                    MsgFoo1D,                   // 8
                    MsgFoo1E,                   // 9
                    MsgFoo2A = BASE2_VAL,       // Uh oh!  7 again...
                    MsgFoo2B                    // Uh oh!  8 again...
                  } FOO;
                  

                  问题是随着代码的增长&随着开发人员向 MsgFoo1x 组添加更多消息,最终它会超出 BASE2_VAL.

                  The problem is that as the code grows & as developers add more messages to the MsgFoo1x group, eventually it overruns BASE2_VAL.

                  这段代码最终会迁移到 C++,所以如果有一个仅限 C++ 的解决方案(模板魔术?),那没问题——但是一个适用于 C 和 C++ 的解决方案更好.

                  This code will eventually be migrated to C++, so if there is a C++-only solution (template magic?), that's OK -- but a solution that works with C and C++ is better.

                  推荐答案

                  有几种方法可以检查此编译时间,但它们可能并不总是适合您.首先在 MsgFoo2A 之前插入一个标记"枚举值.

                  There are a couple ways to check this compile time, but they might not always work for you. Start by inserting a "marker" enum value right before MsgFoo2A.

                  typedef enum
                  {
                      MsgFoo1A = BASE1_VAL,
                      MsgFoo1B,
                      MsgFoo1C,
                      MsgFoo1D,
                      MsgFoo1E,
                      MARKER_1_DONT_USE, /* Don't use this value, but leave it here.  */
                      MsgFoo2A = BASE2_VAL,
                      MsgFoo2B
                  } FOO;
                  

                  现在我们需要一种方法来确保 MARKER_1_DONT_USE 在编译时.有两种常见的技术.

                  Now we need a way to ensure that MARKER_1_DONT_USE < BASE2_VAL at compile-time. There are two common techiques.

                  声明大小为负的数组是错误的.这看起来有点难看,但确实有效.

                  It is an error to declare an array with negative size. This looks a little ugly, but it works.

                  extern int IGNORE_ENUM_CHECK[MARKER_1_DONT_USE > BASE2_VAL ? -1 : 1];
                  

                  如果 MARKER_1_DONT_USE 大于 BASE_2_VAL,几乎所有编写过的编译器都会产生错误.GCC 吐槽:

                  Almost every compiler ever written will generate an error if MARKER_1_DONT_USE is greater than BASE_2_VAL. GCC spits out:

                  test.c:16: error: size of array ‘IGNORE_ENUM_CHECK’ is negative
                  

                  静态断言

                  如果您的编译器支持 C11,您可以使用 _Static_assert.对 C11 的支持并不普遍,但您的编译器无论如何都可能支持 _Static_assert,尤其是因为 C++ 中的相应特性得到广泛支持.

                  Static assertions

                  If your compiler supports C11, you can use _Static_assert. Support for C11 is not ubiquitous, but your compiler may support _Static_assert anyway, especially since the corresponding feature in C++ is widely supported.

                  _Static_assert(MARKER_1_DONT_USE < BASE2_VAL, "Enum values overlap.");
                  

                  GCC 发出以下消息:

                  GCC spits out the following message:

                  test.c:16:1: error: static assertion failed: "Enum values overlap."
                   _Static_assert(MARKER_1_DONT_USE < BASE2_VAL, "Enum values overlap.");
                   ^
                  

                  这篇关于检测多个枚举项何时映射到相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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?)
                  <legend id='H9Hww'><style id='H9Hww'><dir id='H9Hww'><q id='H9Hww'></q></dir></style></legend>

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

                    <tbody id='H9Hww'></tbody>

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