<legend id='4osUT'><style id='4osUT'><dir id='4osUT'><q id='4osUT'></q></dir></style></legend>

      <small id='4osUT'></small><noframes id='4osUT'>

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

      1. <tfoot id='4osUT'></tfoot>

        对于 lambda 来说,本质上来说,它自己关闭是否有效?

        时间:2023-07-01
          • <bdo id='a2FfE'></bdo><ul id='a2FfE'></ul>
              <tfoot id='a2FfE'></tfoot>

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

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

                  本文介绍了对于 lambda 来说,本质上来说,它自己关闭是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这个 lambda 递归是否有效?

                  #include #include int main() {std::functiong = [&g](int k) {返回 (k ? k * g(k-1) : 1);};std::cout <<克(10);//3628800}

                  它似乎可以编译和运行,但我对关闭 g 感到紧张与我初始化它的语句相同.在 1-10 的范围内严格效度...?

                  解决方案

                  在您通过引用捕获 g 时,它已被声明,因此该名称可供使用:<块引用>

                  3.3.2/1 名称的声明点紧跟在其完整声明符(第 8 条)之后和初始化符之前

                  您可以在初始化之前以有限的方式使用对象 - 基本上,任何不依赖于值的东西都可以:

                  <块引用>

                  3.8/6 在对象的生命周期开始之前但在对象将占用的存储空间之后已分配 [...] 任何引用原始对象的泛左值都可以使用,但仅限于有限的方法.[...] 使用不依赖于其值的泛左值的属性是明确定义的.

                  所以根据我的理解,你正在做的事情是明确定义的.

                  (虽然是超迂腐,我不认为它是在分配自动对象的存储时指定的,并且 8.3.2/5 说应初始化引用以引用有效对象"而没有定义有效",所以有理由认为它没有明确定义).

                  Is this lambda recursion valid?

                  #include <functional>
                  #include <iostream>
                  
                  int main() {
                     std::function<int(int)> g = [&g](int k) {
                         return (k ? k * g(k-1) : 1);
                     };
                  
                     std::cout << g(10); // 3628800
                  }
                  

                  It appears to compile and run ok, but I'm nervous about closing over g in the same statement that I initialise it. Strict validity on a scale of 1-10...?

                  解决方案

                  At the point at which you capture g by reference, it has been declared, so the name is available for use:

                  3.3.2/1 The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer

                  You are allowed to use objects in limited ways before they are initialised - basically, anything that doesn't depend on the value is OK:

                  3.8/6 before the lifetime of an object has started but after the storage which the object will occupy has been allocated [...] any glvalue that refers to the original object may be used but only in limited ways. [...] using the properties of the glvalue that do not depend on its value is well-defined.

                  So by my understanding, what you are doing is well-defined.

                  (Although, being ultrapedantic, I don't think it's specified when the storage for an automatic object is allocated, and 8.3.2/5 says that "a reference shall be initialized to refer to a valid object" without defining "valid", so there's scope to argue that it's not well-defined).

                  这篇关于对于 lambda 来说,本质上来说,它自己关闭是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:lambdas 需要捕获“this"来调用静态成员函数? 下一篇:c++11/1y lambda 函数的类型签名是什么?

                  相关文章

                        <bdo id='26yUn'></bdo><ul id='26yUn'></ul>

                      <small id='26yUn'></small><noframes id='26yUn'>

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