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

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

  • <tfoot id='mnaI0'></tfoot>
    <legend id='mnaI0'><style id='mnaI0'><dir id='mnaI0'><q id='mnaI0'></q></dir></style></legend>

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

        Lambda 捕获作为常量引用?

        时间:2023-06-30
        <tfoot id='4HUbv'></tfoot>

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

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

                  本文介绍了Lambda 捕获作为常量引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以通过 lambda 表达式中的 const 引用来捕获?

                  Is it possible to capture by const reference in a lambda expression?

                  我希望下面标记的作业失败,例如:

                  I want the assignment marked below to fail, for example:

                  #include <algorithm>
                  #include <string>
                  
                  using namespace std;
                  
                  int main()
                  {
                      string strings[] = 
                      {
                          "hello",
                          "world"
                      };
                      static const size_t num_strings = sizeof(strings)/sizeof(strings[0]);
                  
                      string best_string = "foo";
                  
                      for_each( &strings[0], &strings[num_strings], [&best_string](const string& s)
                        {
                          best_string = s; // this should fail
                        }
                      );
                  return 0;
                  }
                  

                  更新: 由于这是一个老问题,如果 C++14 中有工具可以帮助解决这个问题,最好更新它.C++14 中的扩展是否允许我们通过常量引用捕获非常量对象?(2015 年 8 月)

                  Update: As this is an old question, it might be good to update it if there are facilities in C++14 to help with this. Do the extensions in C++14 allow us to capture a non-const object by const reference? (August 2015)

                  推荐答案

                  const 不在 n3092 中捕获的语法中:

                  const isn't in the grammar for captures as of n3092:

                  capture:
                    identifier
                    & identifier
                    this
                  

                  文本仅提及按复制捕获和按引用捕获,并未提及任何类型的常量.

                  The text only mention capture-by-copy and capture-by-reference and doesn't mention any sort of const-ness.

                  对我来说感觉像是疏忽,但我并没有非常密切地遵循标准化流程.

                  Feels like an oversight to me, but I haven't followed the standardization process very closely.

                  这篇关于Lambda 捕获作为常量引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:错误:变量“无法隐式捕获,因为未指定默认捕获模式" 下一篇:在 lambda 中捕获完美转发的变量

                  相关文章

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

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

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

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