• <bdo id='1Pgjo'></bdo><ul id='1Pgjo'></ul>

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

    1. <tfoot id='1Pgjo'></tfoot>

      1. <legend id='1Pgjo'><style id='1Pgjo'><dir id='1Pgjo'><q id='1Pgjo'></q></dir></style></legend>

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

      2. 如何将 unique_ptr 捕获到 lambda 表达式中?

        时间:2023-06-30

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

          • <tfoot id='BNJeu'></tfoot>
              <tbody id='BNJeu'></tbody>

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

                <bdo id='BNJeu'></bdo><ul id='BNJeu'></ul>
                1. 本文介绍了如何将 unique_ptr 捕获到 lambda 表达式中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试了以下方法:

                  std::function<void ()> getAction(std::unique_ptr<MyClass> &&psomething){
                      //The caller given ownership of psomething
                      return [psomething](){ 
                          psomething->do_some_thing();
                          //psomething is expected to be released after this point
                      };
                  }
                  

                  但它不编译.有什么想法吗?

                  But it does not compile. Any ideas?

                  更新:

                  建议,需要一些新语法来明确指定我们需要将所有权转移到 lambda,我现在正在考虑以下语法:

                  AS suggested, some new syntax is required to explicitly specify we need to transfer the ownership to the lambda, I am now thinking about the following syntax:

                  std::function<void ()> getAction(std::unique_ptr<MyClass> psomething){
                      //The caller given ownership of psomething
                      return [auto psomething=move(psomething)](){ 
                          psomething->do_some_thing();
                          //psomething is expected to be released after this point
                      };
                  }
                  

                  它会是一个好的候选人吗?

                  Would it be a good candidate?

                  更新 1:

                  我将展示我对 movecopy 的实现如下:

                  I will show my implementation of move and copy as following:

                  template<typename T>
                  T copy(const T &t) {
                      return t;
                  }
                  
                  //process lvalue references
                  template<typename T>
                  T move(T &t) {
                      return std::move(t);
                  }
                  
                  class A{/*...*
                    <i id='GzoDe'><tr id='GzoDe'><dt id='GzoDe'><q id='GzoDe'><span id='GzoDe'><b id='GzoDe'><form id='GzoDe'><ins id='GzoDe'></ins><ul id='GzoDe'></ul><sub id='GzoDe'></sub></form><legend id='GzoDe'></legend><bdo id='GzoDe'><pre id='GzoDe'><center id='GzoDe'></center></pre></bdo></b><th id='GzoDe'></th></span></q></dt></tr></i><div id='GzoDe'><tfoot id='GzoDe'></tfoot><dl id='GzoDe'><fieldset id='GzoDe'></fieldset></dl></div>

                        <bdo id='GzoDe'></bdo><ul id='GzoDe'></ul>
                          <tbody id='GzoDe'></tbody>

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

                        <legend id='GzoDe'><style id='GzoDe'><dir id='GzoDe'><q id='GzoDe'></q></dir></style></legend>

                          <tfoot id='GzoDe'></tfoot>