短的例子:

#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。这应该是标准的)


当前回答

我的印象是 价值捕获的关键在于 允许用户更改临时 —否则,我几乎总是更好地使用引用捕获,而不是 我吗?

N不是暂时的。N是使用lambda表达式创建的lambda-function-对象的成员。默认的期望是,调用你的lambda不会修改它的状态,因此它是const,以防止你意外地修改n。

其他回答

现在有一个建议可以减轻lambda声明中对mutable的需求:n3424

You need to think what is the closure type of your Lambda function. Every time you declare a Lambda expression, the compiler creates a closure type, which is nothing less than an unnamed class declaration with attributes (environment where the Lambda expression where declared) and the function call ::operator() implemented. When you capture a variable using copy-by-value, the compiler will create a new const attribute in the closure type, so you can't change it inside the Lambda expression because it is a "read-only" attribute, that's the reason they call it a "closure", because in some way, you are closing your Lambda expression by copying the variables from upper scope into the Lambda scope. When you use the keyword mutable, the captured entity will became a non-const attribute of your closure type. This is what causes the changes done in the mutable variable captured by value, to not be propagated to upper scope, but keep inside the stateful Lambda. Always try to imagine the resulting closure type of your Lambda expression, that helped me a lot, and I hope it can help you too.

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中应该改变这一点。它很短,写得很好,如果你想知道关于这个特定的特性“委员们在想什么”,值得一读。

我的印象是 价值捕获的关键在于 允许用户更改临时 —否则,我几乎总是更好地使用引用捕获,而不是 我吗?

N不是暂时的。N是使用lambda表达式创建的lambda-function-对象的成员。默认的期望是,调用你的lambda不会修改它的状态,因此它是const,以防止你意外地修改n。

我也想知道为什么[=]需要显式可变的最简单的解释是在这个例子中:

int main()
{
    int x {1};
    auto lbd = [=]() mutable { return x += 5; };
    printf("call1:%d\n", lbd());
    printf("call2:%d\n", lbd());
    return 0;
}

输出:

call1:6
call2:11

单词:

您可以看到在第二次调用时x值是不同的(call1为1,call2为6)。

lambda对象按值保存捕获的变量(有自己的值) 复制),如果[=]。 lambda可以被调用多次。

在一般情况下,我们必须有相同的捕获变量的值,基于已知的捕获值,有相同的可预测的lambda行为,而不是在lambda工作期间更新。这就是为什么默认行为假设为const(预测lambda对象成员的变化),当用户意识到后果时,他会使用mutable来承担这个责任。

与按值捕获相同。举个例子:

auto lbd = [x]() mutable { return x += 5; };