• <bdo id='gkoGI'></bdo><ul id='gkoGI'></ul>
      <tfoot id='gkoGI'></tfoot>

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

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

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

        为什么 C++11 的 lambda 需要“可变"?默认情况下,按值捕获的关键字?

        Why does C++11#39;s lambda require quot;mutablequot; keyword for capture-by-value, by default?(为什么 C++11 的 lambda 需要“可变?默认情况下,按值捕获的关键字?)

        <small id='6imIs'></small><noframes id='6imIs'>

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

          1. <tfoot id='6imIs'></tfoot>

                <bdo id='6imIs'></bdo><ul id='6imIs'></ul>
                  本文介绍了为什么 C++11 的 lambda 需要“可变"?默认情况下,按值捕获的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  简短示例:

                  #include <iostream>
                  
                  int main()
                  {
                      int n;
                      [&](){n = 10;}();             // OK
                      [=]() mutable {n = 20;}();    // OK
                      // [=](){n = 10;}();          // Error: a by-value capture cannot be modified in a non-mutable lambda
                      std::cout << n << "
                  ";       // "10"
                  }
                  

                  问题:为什么我们需要 mutable 关键字?它与传统的参数传递到命名函数有很大不同.背后的原理是什么?

                  The question: Why do we need the mutable keyword? It's quite different from traditional parameter passing to named functions. What's the rationale behind?

                  我的印象是,按值捕获的全部意义在于允许用户更改临时值——否则我几乎总是最好使用按引用捕获,不是吗?

                  I was under the impression that the whole point of capture-by-value is to allow the user to change the temporary -- otherwise I'm almost always better off using capture-by-reference, aren't I?

                  有什么启示吗?

                  (顺便说一下,我正在使用 MSVC2010.AFAIK 这应该是标准的)

                  (I'm using MSVC2010 by the way. AFAIK this should be standard)

                  推荐答案

                  它需要 mutable 因为默认情况下,函数对象每次被调用时都应该产生相同的结果.这就是面向对象的函数和有效地使用全局变量的函数之间的区别.

                  It requires mutable because by default, a function object should produce the same result every time it's called. This is the difference between an object orientated function and a function using a global variable, effectively.

                  这篇关于为什么 C++11 的 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++程序中的堆栈溢出错误)
                    <tbody id='74aVr'></tbody>

                      <small id='74aVr'></small><noframes id='74aVr'>

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