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

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

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

    1. <tfoot id='HgEpw'></tfoot>

          <bdo id='HgEpw'></bdo><ul id='HgEpw'></ul>
      1. 静态 constexpr int 与老式枚举:何时以及为什么?

        Static constexpr int vs old-fashioned enum: when and why?(静态 constexpr int 与老式枚举:何时以及为什么?)

          <bdo id='zVtJd'></bdo><ul id='zVtJd'></ul>

          • <small id='zVtJd'></small><noframes id='zVtJd'>

              <tfoot id='zVtJd'></tfoot>
                <tbody id='zVtJd'></tbody>
              <legend id='zVtJd'><style id='zVtJd'><dir id='zVtJd'><q id='zVtJd'></q></dir></style></legend>

                  <i id='zVtJd'><tr id='zVtJd'><dt id='zVtJd'><q id='zVtJd'><span id='zVtJd'><b id='zVtJd'><form id='zVtJd'><ins id='zVtJd'></ins><ul id='zVtJd'></ul><sub id='zVtJd'></sub></form><legend id='zVtJd'></legend><bdo id='zVtJd'><pre id='zVtJd'><center id='zVtJd'></center></pre></bdo></b><th id='zVtJd'></th></span></q></dt></tr></i><div id='zVtJd'><tfoot id='zVtJd'></tfoot><dl id='zVtJd'><fieldset id='zVtJd'></fieldset></dl></div>
                  本文介绍了静态 constexpr int 与老式枚举:何时以及为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这可能是一个基本问题,但我现在看不到自己的回复.

                  This is maybe a basic question, but I cannot see the response by myself right now.

                  考虑以下代码:

                  template<bool b>
                  struct T {
                      static constexpr int value = (b ? 42 : 0);
                  };
                  
                  template<bool b>
                  struct U {
                      enum { value = (b ? 42 : 0) };
                  };
                  
                  int main() {
                      static_assert(T<true>::value == 42, "!");
                      static_assert(T<false>::value == 0, "!");
                      static_assert(U<true>::value == 42, "!");
                      static_assert(U<false>::value == 0, "!");
                  }
                  

                  我习惯于使用像 T 这样的结构,但我不止一次看到像 U 这样的结构用于相同的目的(主要是特征定义).

                  I'm used to using structs like T, but more than once I've seen structs like U used for the same purpose (mostly traits definition).

                  据我所知,它们都是在编译时解决的,它们解决了几乎相同的问题,但在我看来 TU 更具可读性代码>(好吧,我知道,我的个人意见).

                  As far as I can see, they are both resolved at compile time and they solve almost the same problem, but it seems to me that T is far more readable than U (well, I know, my personal opinion).

                  我的问题很简单:有什么技术原因可以证明一种解决方案比另一种更好吗?
                  更重要的是,是否有任何一种情况不是可行的解决方案?

                  My question is pretty simple: is there any technical reason for which one solution is better than the other one?
                  Even more, is there any case for which one of them is not a viable solution?

                  推荐答案

                  请注意,以下答案不适用于 C++ 17 及更高版本.

                  这样使用时,积分常数不会有明显差异.

                  There will be no noticeable difference for integral constants when used like this.

                  然而,enum 实际上更好,因为它是一个真正的命名常量.constexpr 整数常量是一个对象,例如可以被 ODR 使用 - 这会导致链接错误.

                  However, enum is actually better, because it is a true named constant. constexpr integral constant is an object which can be, for example, ODR-used - and that would result in linking errors.

                  #include <iostream>
                  
                  struct T {
                      static constexpr int i = 42;
                      enum : int {x = 42};
                  };
                  
                  void check(const int& z) {
                      std::cout << "Check: " << z << "
                  ";
                  }
                  
                  int main() {
                      // check(T::i); // Uncommenting this will lead to link error
                      check(T::x);
                  }
                  

                  check(T::i)被取消注释时,程序无法链接:

                  When check(T::i) is uncommented, the program can not be linked:

                  /tmp/ccZoETx7.o:在函数`main'中:ccc.cpp:(.text+0x45): 未定义引用 `T::i' collect2:错误:ld 返回 1 退出状态

                  /tmp/ccZoETx7.o: In function `main': ccc.cpp:(.text+0x45): undefined reference to `T::i' collect2: error: ld returned 1 exit status

                  然而,真正的 enum 总是有效的.

                  However, the true enum always works.

                  这篇关于静态 constexpr int 与老式枚举:何时以及为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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?)

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

                    • <bdo id='7ii3g'></bdo><ul id='7ii3g'></ul>

                        <small id='7ii3g'></small><noframes id='7ii3g'>

                          <tbody id='7ii3g'></tbody>
                        • <tfoot id='7ii3g'></tfoot>

                          <legend id='7ii3g'><style id='7ii3g'><dir id='7ii3g'><q id='7ii3g'></q></dir></style></legend>