<legend id='oxE10'><style id='oxE10'><dir id='oxE10'><q id='oxE10'></q></dir></style></legend>
    • <bdo id='oxE10'></bdo><ul id='oxE10'></ul>

      <tfoot id='oxE10'></tfoot>
    1. <small id='oxE10'></small><noframes id='oxE10'>

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

        模板元编程 - 使用 Enum Hack 和 Static Const 的区别

        时间:2023-05-25

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

              <small id='1T1U3'></small><noframes id='1T1U3'>

                <legend id='1T1U3'><style id='1T1U3'><dir id='1T1U3'><q id='1T1U3'></q></dir></style></legend>
                  <bdo id='1T1U3'></bdo><ul id='1T1U3'></ul>
                  本文介绍了模板元编程 - 使用 Enum Hack 和 Static Const 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想知道在使用模板元编程技术时使用静态常量和枚举黑客有什么区别.

                  I'm wondering what the difference is between using a static const and an enum hack when using template metaprogramming techniques.

                  EX:(斐波那契通过 TMP)

                  EX: (Fibonacci via TMP)

                  template< int n > struct TMPFib {
                    static const int val =
                      TMPFib< n-1 >::val + TMPFib< n-2 >::val;
                  };
                  
                  template<> struct TMPFib< 1 > {
                    static const int val = 1;
                  };
                  
                  template<> struct TMPFib< 0 > {
                    static const int val = 0;
                  };
                  

                  对比

                  template< int n > struct TMPFib {
                    enum {
                      val = TMPFib< n-1 >::val + TMPFib< n-2 >::val
                    };
                  };
                  
                  template<> struct TMPFib< 1 > {
                    enum { val = 1 };
                  };
                  
                  template<> struct TMPFib< 0 > {
                    enum { val = 0 };
                  };
                  

                  为什么要使用一个?我已经读到在类内部支持静态常量之前使用了 enum hack,但为什么现在使用它?

                  Why use one over the other? I've read that the enum hack was used before static const was supported inside classes, but why use it now?

                  推荐答案

                  枚举不是 lval,静态成员值是,如果通过引用传递模板将被实例化:

                  Enums aren't lvals, static member values are and if passed by reference the template will be instanciated:

                  void f(const int&);
                  f(TMPFib<1>::value);
                  

                  如果你想做纯编译时间计算等,这是一个不希望的副作用.

                  If you want to do pure compile time calculations etc. this is an undesired side-effect.

                  主要的历史差异是枚举也适用于不支持成员值的类内初始化的编译器,现在大多数编译器都应该修复这个问题.
                  枚举和静态常量的编译速度也可能存在差异.

                  The main historic difference is that enums also work for compilers where in-class-initialization of member values is not supported, this should be fixed in most compilers now.
                  There may also be differences in compilation speed between enum and static consts.

                  boost 编码指南和旧线程 在有关该主题的 boost 档案中.

                  There are some details in the boost coding guidelines and an older thread in the boost archives regarding the subject.

                  这篇关于模板元编程 - 使用 Enum Hack 和 Static Const 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:带有模板成员变量的 C++ 类 下一篇:默认模板参数偏特化

                  相关文章

                  最新文章

                • <tfoot id='ZVY9D'></tfoot>

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

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