短的例子:
#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 << "\n"; // "10"
}
问题是:为什么我们需要mutable关键字?它与传统的参数传递到命名函数有很大不同。背后的原理是什么?
在我的印象中,按值捕获的全部意义在于允许用户更改临时对象——否则我几乎总是使用按引用捕获更好,不是吗?
有什么启示吗?
(顺便说一下,我用的是MSVC2010。这应该是标准的)
你必须理解捕获的意思!这是捕获而不是参数传递!让我们看一些代码示例:
int main()
{
using namespace std;
int x = 5;
int y;
auto lamb = [x]() {return x + 5; };
y= lamb();
cout << y<<","<< x << endl; //outputs 10,5
x = 20;
y = lamb();
cout << y << "," << x << endl; //output 10,20
}
正如你所看到的,即使x被更改为20,仍然返回10 (x在lambda中仍然是5)
在lambda内部更改x意味着在每次调用中更改lambda本身(lambda在每次调用中都发生突变)。为了加强正确性,标准引入了mutable关键字。通过将lambda指定为mutable,就意味着对lambda的每次调用都可能导致lambda本身的更改。让我们看另一个例子:
int main()
{
using namespace std;
int x = 5;
int y;
auto lamb = [x]() mutable {return x++ + 5; };
y= lamb();
cout << y<<","<< x << endl; //outputs 10,5
x = 20;
y = lamb();
cout << y << "," << x << endl; //outputs 11,20
}
上面的例子表明,通过使lambda可变,在lambda内部改变x会在每次调用时用一个新的x值“突变”lambda,而这个x值与主函数中x的实际值没有任何关系
FWIW, c++标准化委员会的知名成员Herb Sutter在Lambda正确性和可用性问题中给出了不同的答案:
Consider this straw man example, where the programmer captures a local variable by
value and tries to modify the
captured value (which is a member variable of the lambda object):
int val = 0;
auto x = [=](item e) // look ma, [=] means explicit copy
{ use(e,++val); }; // error: count is const, need ‘mutable’
auto y = [val](item e) // darnit, I really can’t get more explicit
{ use(e,++val); }; // same error: count is const, need ‘mutable’
This feature appears to have been added out of a concern that the user
might not realize he got a copy, and in particular that since lambdas
are copyable he might be changing a different lambda’s copy.
他的论文是关于为什么在c++ 14中应该改变这一点。它很短,写得很好,如果你想知道关于这个特定的特性“委员们在想什么”,值得一读。
为了扩展Puppy的回答,lambda函数被设计为纯函数。这意味着给定唯一输入集的每次调用总是返回相同的输出。让我们将input定义为调用lambda时所有参数加上所有捕获变量的集合。
在纯函数中,输出完全依赖于输入,而不依赖于某些内部状态。因此,任何lambda函数,如果是纯的,不需要改变其状态,因此是不可变的。
When a lambda captures by reference, writing on captured variables is a strain on the concept of pure function, because all a pure function should do is return an output, though the lambda does not certainly mutate because the writing happens to external variables. Even in this case a correct usage implies that if the lambda is called with the same input again, the output will be the same everytime, despite these side effects on by-ref variables. Such side effects are just ways to return some additional input (e.g. update a counter) and could be reformulated into a pure function, for example returning a tuple instead of a single value.
在我的印象中,按值捕获的全部意义在于允许用户更改临时对象——否则我几乎总是使用按引用捕获更好,不是吗?
问题是,它是“几乎”吗?一个常见的用例似乎是返回或传递lambdas:
void registerCallback(std::function<void()> f) { /* ... */ }
void doSomething() {
std::string name = receiveName();
registerCallback([name]{ /* do something with name */ });
}
我认为mutable不是almost的例子。我认为“按值捕获”就像“允许我在捕获的实体死亡后使用它的值”,而不是“允许我更改它的副本”。但这或许是有争议的。