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

    • <bdo id='bK9W9'></bdo><ul id='bK9W9'></ul>
  • <tfoot id='bK9W9'></tfoot>

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

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

        lambda 函数/表达式是否支持 constexpr?

        时间:2023-06-30

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

                  <bdo id='pUwX2'></bdo><ul id='pUwX2'></ul>
                • <small id='pUwX2'></small><noframes id='pUwX2'>

                  <tfoot id='pUwX2'></tfoot>
                  本文介绍了lambda 函数/表达式是否支持 constexpr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  struct Test
                  {
                    static const int value = []() -> int { return 0; } ();
                  };
                  

                  使用 gcc-4.6 我得到类似错误:函数需要是 constexpr.我尝试了将 constexpr 放在不同地方的多种组合,但没有运气.

                  With gcc-4.6 I get something like, error: function needs to be constexpr. I have tried multiple combinations of putting constexpr at various places, but no luck.

                  constexpr 是否也支持 lambda 函数(无论是否指定了 return 类型)?什么是正确的语法?

                  Is constexpr supported for lambda functions as well (irrespective of return type specified or not) ? What is the correct syntax ?

                  任何可能的解决方法?

                  推荐答案

                  更新:从 C++17 开始,常量表达式中允许使用 lambda.

                  Update: As of C++17, lambdas are permitted in constant expressions.

                  根据 [expr.const]/(2.6) 目前 (C++14) 不允许在常量表达式中使用 Lambda,但它们将一次 N4487被接受(可以在N4582工作草案中找到):

                  Lambdas are currently (C++14) not allowed in constant expressions as per [expr.const]/(2.6), but they will once N4487 is accepted (which can be found in the working draft N4582):

                  这个提议建议在常量中允许 lambda 表达式表达式,删除现有的限制.作者提出某些 lambda 表达式和某些闭包上的操作允许对象出现在常量表达式中.在这样做,我们还建议将闭包类型视为文字类型,如果它的每个数据成员的类型都是文字类型;并且,如果constexpr 说明符在 lambda 声明符中被省略,即生成的函数调用运算符是 constexpr 如果它满足constexpr 函数的要求(类似于constexpr 对于隐式定义已经发生的推断构造函数和赋值运算符函数).

                  This proposal suggests allowing lambda-expressions in constant expressions, removing an existing restriction. The authors propose that certain lambda-expressions and operations on certain closure objects be allowed to appear within constant expressions. In doing so, we also propose that a closure type be considered a literal type if the type of each of its data-members is a literal type; and, that if the constexpr specifier is omitted within the lambda-declarator, that the generated function call operator be constexpr if it would satisfy the requirements of a constexpr function (similar to the constexpr inference that already occurs for implicitly defined constructors and the assignment operator functions).

                  这篇关于lambda 函数/表达式是否支持 constexpr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++0x lambda 按值捕获总是常量? 下一篇:在 C++11 lambda 中通过引用捕获引用

                  相关文章

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

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