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

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

      2. 在同一个类中使用 constexpr 作为模板参数时出错

        时间:2023-10-18
            <bdo id='Mnwmh'></bdo><ul id='Mnwmh'></ul>

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

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

                    <tbody id='Mnwmh'></tbody>
                  <tfoot id='Mnwmh'></tfoot>
                  本文介绍了在同一个类中使用 constexpr 作为模板参数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我尝试编译以下 C++0x 代码,则会出现错误:

                  If I try to compile the following C++0x code, I get an error:

                  template<int n> struct foo { };
                  
                  struct bar {
                      static constexpr int number() { return 256; }
                  
                      void function(foo<number()> &);
                  };
                  

                  使用 gcc 4.6.1,错误信息是:

                  With gcc 4.6.1, the error message is:

                  test.cc:6:27: error: ‘static constexpr int bar::number()’ used before its definition
                  test.cc:6:28: note: in template argument for type ‘int’
                  

                  使用 clang 2.8,错误信息是:

                  With clang 2.8, the error message is:

                  test.cc:6:20: error: non-type template argument of type 'int' is not an integral
                        constant expression
                          void function(foo<number()> &);
                                            ^~~~~~~~
                  1 error generated.
                  

                  如果我将 constexpr 函数移动到基类,它在 gcc 上工作,并在 clang 上给出相同的错误消息:

                  If I move the constexpr function to a base class, it works on gcc, and gives the same error message on clang:

                  template<int n> struct foo { };
                  
                  struct base {
                      static constexpr int number() { return 256; }
                  };
                  
                  struct bar : base {
                      void function(foo<number()> &);
                  };
                  

                  是代码错误,还是 gcc 4.6 对 C++0x 实现的限制或错误?如果代码错了,为什么错了,C++11标准的哪些条款说错了?

                  Is the code wrong, or is it a limitation or bug on gcc 4.6's implementation of C++0x? If the code is wrong, why is it wrong, and which clauses of the C++11 standard say it is incorrect?

                  推荐答案

                  在 C++ 中,类的成员函数的内联定义仅在类中的每个声明都被解析后才被解析.因此,在您的第一个示例中,编译器无法在声明 function() 的位置看到 number() 的定义.

                  In C++, inline definitions of member functions for a class are only parsed after every declaration in the class is parsed. Therefore, in your first example, the compiler can't see the definition of number() at the point where function() is declared.

                  (clang 的发布版本不支持计算 constexpr 函数,所以你的测试用例都不会在那里工作.)

                  (No released version of clang has support for evaluating constexpr functions, so none of your testcases will work there.)

                  这篇关于在同一个类中使用 constexpr 作为模板参数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在编译时验证 c/c++ 预处理宏中的参数是否为 ARRAY 类型 下一篇:命名空间 std 中没有名为 stoi 的成员

                  相关文章

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

                  <legend id='xg70h'><style id='xg70h'><dir id='xg70h'><q id='xg70h'></q></dir></style></legend>

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

                    • <bdo id='xg70h'></bdo><ul id='xg70h'></ul>

                  2. <tfoot id='xg70h'></tfoot>