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

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

    <legend id='dsNKv'><style id='dsNKv'><dir id='dsNKv'><q id='dsNKv'></q></dir></style></legend>
    <tfoot id='dsNKv'></tfoot>
      1. 如果函数在类范围内声明,则 constexpr 不起作用

        时间:2023-10-18

        <tfoot id='XTkAc'></tfoot>

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

                  本文介绍了如果函数在类范围内声明,则 constexpr 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用的是 g++4.8.0,它不包含早期的 constexpr 错误.因此下面的代码工作罚款:

                  I am using g++4.8.0, which doesn't contain earlier constexpr bug. Thus below code works fine:

                  constexpr int size() { return 5; }
                  int array[size()];
                  
                  int main () {}
                  

                  但是,如果我将 class 中的变量都作为 static 括起来,那么它会给出 编译器错误:

                  However, if I enclose both the variable inside a class as static, then it gives compiler error:

                  struct X {
                    constexpr static int size() { return 5; }
                    static const int array[size()]; 
                  };
                  
                  int main () {}
                  

                  这里是错误:

                  错误:数组‘array’的大小不是整数常量表达式

                  error: size of array ‘array’ is not an integral constant-expression

                  是否禁止以这种方式使用 constexpr 或另一个 g++ 错误?

                  Is it forbidden to use constexpr in such a way or yet another g++ bug?

                  推荐答案

                  是的,格式不正确.原因如下:

                  Yes, it is ill-formed. Here's why:

                  constexpr 函数在用于常量表达式之前需要定义(不仅仅是声明).

                  A constexpr function needs to be defined (not just declared) before being used in a constant expression.

                  例如:

                  constexpr int f(); // declare f
                  constexpr int x = f(); // use f - ILLEGAL, f not defined
                  constexpr int f() { return 5; } // define f, too late
                  

                  类说明符内的函数定义(以及初始值设定项和默认参数)基本上按照它们在类外定义的顺序进行解析.

                  function definitions inside a class specifier (as well as initializers and default parameters) are essentially parsed in an order like they were defined outside the class.

                  所以:

                  struct X {
                    constexpr static int size() { return 5; }
                    static const int array[size()]; 
                  };
                  

                  按以下顺序解析:

                  struct X {
                     constexpr inline static int size(); // function body defered
                     static const int array[size()];  // <--- POINT A
                  };
                  
                  constexpr inline int X::size() { return 5; }
                  

                  也就是说,函数体的解析被推迟到类说明符之后.

                  That is, parsing of function bodies are defered until after the class specifier.

                  推迟函数体解析的目的是为了让函数体可以转发引用当时尚未声明的类成员,并且它们可以将自己的类用作完整类型:

                  The purpose of this deferral of function body parsing is so that function bodies can forward reference class members not yet declared at that point, and also so they can use their own class as a complete type:

                  struct X
                  {
                      void f() { T t; /* OK */ }
                      typedef int T;
                  };
                  

                  与在命名空间范围内相比:

                  Compared to at namespace scope:

                  void f() { T t; /* error, T not declared */ }
                  typedef int T;
                  

                  POINT A,编译器还没有size()的定义,所以不能调用.对于编译时性能 constexpr 函数需要在编译期间被调用之前在翻译单元中使用之前定义,否则编译器将不得不进行多次传递以链接"常量表达式评价.

                  At POINT A, the compiler doesn't have the definition of size() yet, so it can't call it. For compile-time performance constexpr functions need to be defined ahead of their use in the translation unit before being called during compile, otherwise the compiler would have to make a multiple passes just to "link" constant expressions for evaluation.

                  这篇关于如果函数在类范围内声明,则 constexpr 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ 中的额外限定错误 下一篇:为什么语句不能出现在命名空间范围内?

                  相关文章

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

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

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

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