• <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 不起作用

        constexpr not working if the function is declared inside class scope(如果函数在类范围内声明,则 constexpr 不起作用)

        <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 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                  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>

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