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

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

        <bdo id='TzhBq'></bdo><ul id='TzhBq'></ul>
    1. <legend id='TzhBq'><style id='TzhBq'><dir id='TzhBq'><q id='TzhBq'></q></dir></style></legend>

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

      2. 在 lambda 中捕获完美转发的变量

        时间:2023-06-30

          <tfoot id='S1UNV'></tfoot>
            <bdo id='S1UNV'></bdo><ul id='S1UNV'></ul>

            1. <small id='S1UNV'></small><noframes id='S1UNV'>

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

                1. <legend id='S1UNV'><style id='S1UNV'><dir id='S1UNV'><q id='S1UNV'></q></dir></style></legend>
                  本文介绍了在 lambda 中捕获完美转发的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  template<typename T> void doSomething(T&& mStuff)
                  {
                      auto lambda([&mStuff]{ doStuff(std::forward<T>(mStuff)); });
                      lambda();
                  }
                  

                  使用 &mStuff 语法捕获完美转发的 mStuff 变量是否正确?

                  Is it correct to capture the perfectly-forwarded mStuff variable with the &mStuff syntax?

                  或者对于完美转发的变量是否有特定的捕获语法?

                  Or is there a specific capture syntax for perfectly-forwarded variables?

                  如果完美转发的变量是参数包怎么办?

                  推荐答案

                  捕获完美转发的 mStuff 变量是否正确&mStuff 语法?

                  Is it correct to capture the perfectly-forwarded mStuff variable with the &mStuff syntax?

                  是的,假设您不在 doSomething 之外使用此 lambda.您的代码捕获每个引用的 mStuff 并将其正确转发到 lambda 中.

                  Yes, assuming that you don't use this lambda outside doSomething. Your code captures mStuff per reference and will correctly forward it inside the lambda.

                  对于作为参数包的 mStuff,使用带有包扩展的简单捕获就足够了:

                  For mStuff being a parameter pack it suffices to use a simple-capture with a pack-expansion:

                  template <typename... T> void doSomething(T&&... mStuff)
                  {
                      auto lambda = [&mStuff...]{ doStuff(std::forward<T>(mStuff)...); };
                  }
                  

                  lambda 捕获每个引用的 mStuff 的每个元素.闭包对象为每个参数保存一个左值引用,无论其值类别如何.完美转发仍然有效;事实上,甚至没有区别,因为命名的右值引用无论如何都是左值.

                  The lambda captures every element of mStuff per reference. The closure-object saves an lvalue reference for to each argument, regardless of its value category. Perfect forwarding still works; In fact, there isn't even a difference because named rvalue references would be lvalues anyway.

                  这篇关于在 lambda 中捕获完美转发的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Lambda 捕获作为常量引用? 下一篇:为什么在未计算的操作数中不允许使用 lambda 表达式,但在常量表达式的未计算部分中允许使用 lambda 表达式?

                  相关文章

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

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

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

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