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

    <tfoot id='AohTk'></tfoot>

      • <bdo id='AohTk'></bdo><ul id='AohTk'></ul>

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

        错误:变量“无法隐式捕获,因为未指定默认捕获模式"

        时间:2023-06-30

            <tbody id='29eMZ'></tbody>

            <bdo id='29eMZ'></bdo><ul id='29eMZ'></ul>
          • <small id='29eMZ'></small><noframes id='29eMZ'>

              <legend id='29eMZ'><style id='29eMZ'><dir id='29eMZ'><q id='29eMZ'></q></dir></style></legend>
            1. <tfoot id='29eMZ'></tfoot>
                  <i id='29eMZ'><tr id='29eMZ'><dt id='29eMZ'><q id='29eMZ'><span id='29eMZ'><b id='29eMZ'><form id='29eMZ'><ins id='29eMZ'></ins><ul id='29eMZ'></ul><sub id='29eMZ'></sub></form><legend id='29eMZ'></legend><bdo id='29eMZ'><pre id='29eMZ'><center id='29eMZ'></center></pre></bdo></b><th id='29eMZ'></th></span></q></dt></tr></i><div id='29eMZ'><tfoot id='29eMZ'></tfoot><dl id='29eMZ'><fieldset id='29eMZ'></fieldset></dl></div>
                  本文介绍了错误:变量“无法隐式捕获,因为未指定默认捕获模式"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试遵循这个例子使用带有 remove_if 的 lambda.这是我的尝试:

                  I am trying to follow this example to use a lambda with remove_if. Here is my attempt:

                  int flagId = _ChildToRemove->getId();
                  auto new_end = std::remove_if(m_FinalFlagsVec.begin(), m_FinalFlagsVec.end(), 
                          [](Flag& device) { 
                              return device.getId() == flagId; 
                          });
                  
                  m_FinalFlagsVec.erase(new_end, m_FinalFlagsVec.end());
                  

                  但这无法编译:

                  error C3493: 'flagId' cannot be implicitly captured because no default capture mode has been specified
                  

                  如何在 lambda 表达式中包含外部参数 flagId?

                  How can I include the outside parameter, flagId, in the lambda expression?

                  推荐答案

                  您必须指定要捕获的 flagId.这就是 [] 部分的用途.现在它没有捕获任何东西.您可以按值或按引用捕获(更多信息).类似的东西:

                  You must specify flagId to be captured. That is what the [] part is for. Right now it doesn't capture anything. You can capture (more info) by value or by reference. Something like:

                  auto new_end = std::remove_if(m_FinalFlagsVec.begin(), m_FinalFlagsVec.end(),
                          [&flagId](Flag& device)
                      { return device.getId() == flagId; });
                  

                  通过引用捕获.如果你想通过 const 值捕获,你可以这样做:

                  Which captures by reference. If you want to capture by const value, you can do this:

                  auto new_end = std::remove_if(m_FinalFlagsVec.begin(), m_FinalFlagsVec.end(),
                          [flagId](Flag& device)
                      { return device.getId() == flagId; });
                  

                  或者通过可变值:

                  auto new_end = std::remove_if(m_FinalFlagsVec.begin(), m_FinalFlagsVec.end(),
                          [flagId](Flag& device) mutable
                      { return device.getId() == flagId; });
                  

                  遗憾的是,没有直接的方法可以通过常量引用进行捕获.我个人只会声明一个临时的 const ref 并通过 ref 捕获它:

                  Sadly there is no straightforward way to capture by const reference. I personally would just declare a temporary const ref and capture that by ref:

                  const auto& tmp = flagId;
                  auto new_end = std::remove_if(m_FinalFlagsVec.begin(), m_FinalFlagsVec.end(),
                              [&tmp](Flag& device)
                          { return device.getId() == tmp; }); //tmp is immutable
                  

                  这篇关于错误:变量“无法隐式捕获,因为未指定默认捕获模式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Lambda 隐式捕获因从结构化绑定声明的变量而失败 下一篇:Lambda 捕获作为常量引用?

                  相关文章

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

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

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