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

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

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

        我能在模板参数中声明一个 constexpr lambda 吗?

        时间:2023-05-24

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

                • <small id='iDB53'></small><noframes id='iDB53'>

                    <tbody id='iDB53'></tbody>
                  本文介绍了我能在模板参数中声明一个 constexpr lambda 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我知道这就像打开潘多拉盒子,但它并没有停止打扰我.考虑一个简单的例子:

                  I know it's like opening the Pandora box but it doesn't stop bothering me. Consider a simple example:

                  #include <type_traits>
                  
                  template <auto>
                  struct Foo: std::false_type { };
                  
                  template <>
                  struct Foo<[](){return 1;}()>:std::true_type { };
                  
                  int main() {
                      static_assert(Foo<1>::value);
                  }
                  

                  我知道 lambdas 不能在未评估的上下文中声明,但显然这里不是这种情况.还有什么更奇怪的 clang 5.0.0(我猜它首先部分支持 constexpr lambda)编译它.

                  I know lambdas cannot be declared inside unevaluated context, but obviously this is not the case here. What is even more weird clang 5.0.0 (which, I guess, first partially supports constexpr lambda) does compile it.

                  这是编译器错误还是 C++17 允许这样做?

                  Is it a compiler bug or will C++17 allow this?

                  推荐答案

                  不,这是一个编译器错误.gcc 7.1 正确拒绝代码.

                  No, that is a compiler bug. gcc 7.1 correctly rejects the code.

                  [expr.prim.lambda]/2:

                  lambda 表达式是一个纯右值,其结果对象称为闭包对象.lambda 表达式不得出现在未求值的操作数、模板参数、别名声明、typedef 声明或函数声明中或函数体外的函数模板声明中和默认参数.

                  A lambda-expression is a prvalue whose result object is called the closure object. A lambda-expression shall not appear in an unevaluated operand, in a template-argument, in an alias-declaration, in a typedef declaration, or in the declaration of a function or function template outside its function body and default arguments.

                  从我标记为粗体的部分可以看出,lambda 表达式不能出现在模板参数列表中.

                  As you can see from the part that I marked as bold, a lambda expression cannot appear in a template argument list.

                  这在随后的注释中也有明确说明:

                  This is also made clear in a subsequent note:

                  [ 注意:目的是防止 lambda 出现在签名中.— 尾注 ]

                  [ Note: The intention is to prevent lambdas from appearing in a signature. — end note ]

                  如果我猜的话,我会说这个错误是因为从 C++17 开始,lambdas 是隐式的 constexpr,这使得它们在编译时表达式中被调用是有效的,比如模板参数.但实际上在模板参数中定义 lambda 仍然是非法的.

                  If I were to guess, I would say that the bug comes about because starting with C++17, lambdas are implicitly constexpr, which makes them valid to be called in compile time expressions, like template arguments. But actually defining a lambda in a template argument is still illegal.

                  请注意,此限制已在 C++20 中取消.:)

                  Note that this restriction has been lifted in C++20. :)

                  这篇关于我能在模板参数中声明一个 constexpr lambda 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:“永久"标准::设置 下一篇:删除“通用"的专用模板函数案例无法使用

                  相关文章

                  最新文章

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

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