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

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

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

      <legend id='ozPTy'><style id='ozPTy'><dir id='ozPTy'><q id='ozPTy'></q></dir></style></legend>
        <tfoot id='ozPTy'></tfoot>

      1. C++ lambda 表达式的生命周期是多少?

        What is the lifetime of a C++ lambda expression?(C++ lambda 表达式的生命周期是多少?)
        • <small id='0OUUJ'></small><noframes id='0OUUJ'>

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

                <tbody id='0OUUJ'></tbody>
                <bdo id='0OUUJ'></bdo><ul id='0OUUJ'></ul>
                <legend id='0OUUJ'><style id='0OUUJ'><dir id='0OUUJ'><q id='0OUUJ'></q></dir></style></legend><tfoot id='0OUUJ'></tfoot>
                  本文介绍了C++ lambda 表达式的生命周期是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  (我已经阅读了 什么是生命周期C++ 中的 lambda 派生隐式函子? 已经,它没有回答这个问题.)

                  (I have read What is the lifetime of lambda-derived implicit functors in C++? already and it does not answer this question.)

                  我了解 C++ lambda 语法只是用于创建具有调用运算符和某些状态的匿名类的实例的糖,并且我了解该状态的生命周期要求(取决于您是否通过引用的值捕获.)但是 lambda 对象本身的生命周期是多少?在以下示例中,返回的 std::function 实例有用吗?

                  I understand that C++ lambda syntax is just sugar for making an instance of an anonymous class with a call operator and some state, and I understand the lifetime requirements of that state (decided by whether you capture by value of by reference.) But what is the lifetime of the lambda object itself? In the following example, is the std::function instance returned going to be useful?

                  std::function<int(int)> meta_add(int x) {
                      auto add = [x](int y) { return x + y; };
                      return add;
                  }
                  

                  如果是,它是如何工作的?这对我来说似乎有点太神奇了 - 我只能想象它通过 std::function 复制我的整个实例来工作,这取决于我捕获的内容可能非常繁重 - 过去我已经std::function 主要用于裸函数指针,复制它们很快.鉴于 std::function 的类型擦除,它似乎也有问题.

                  If it is, how does it work? This seems a bit too much magic to me - I can only imagine it working by std::function copying my whole instance, which could be very heavy depending on what I captured - in the past I've used std::function primarily with bare function pointers, and copying those is quick. It also seems problematic in light of std::function's type erasure.

                  推荐答案

                  生命周期正是如果你用一个手卷函子替换你的 lambda 的生命周期:

                  The lifetime is exactly what it would be if you replaced your lambda with a hand-rolled functor:

                  struct lambda {
                     lambda(int x) : x(x) { }
                     int operator ()(int y) { return x + y; }
                  
                  private:
                     int x;
                  };
                  
                  std::function<int(int)> meta_add(int x) {
                     lambda add(x);
                     return add;
                  }
                  

                  对象将被创建,局部于 meta_add 函数,然后移动 [in itsentirty,包括 x 的值] 到返回值,然后本地实例将超出范围并正常销毁.但是,只要持有它的 std::function 对象有效,从函数返回的对象就会一直有效.这显然取决于调用上下文.

                  The object will be created, local to the meta_add function, then moved [in its entirty, including the value of x] into the return value, then the local instance will go out of scope and be destroyed as normal. But the object returned from the function will remain valid for as long as the std::function object that holds it does. How long that is obviously depends on the calling context.

                  这篇关于C++ lambda 表达式的生命周期是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                      <bdo id='43u5U'></bdo><ul id='43u5U'></ul>
                    • <tfoot id='43u5U'></tfoot>
                      <legend id='43u5U'><style id='43u5U'><dir id='43u5U'><q id='43u5U'></q></dir></style></legend>

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