Lambdas behave more or less like function objects; as a functional object, they have a function call operator, i.e. operator() . For mutable lambdas, this const function:
[expr.prim.lambda]
5 The closure type for a nonequivalent lambda expression has an inline function call operator [...] This function call operator or operator template is declared const (9.3.1) if and only if the lambda expression parameter-declaration-sentence is not mutable .
Since the objects captured by the copy behave as if they are lambda member variables:
15 [...] For each object captured by the copy, an unnamed non-static data element is declared in the close type.
and non- mutable members cannot be changed inside the const member function ([class.this] / 1, [dcl.type.cv] / 4), if you want to modify the captured objects you need to declare lambda mutable .
In a way, your lambda looks like this:
class Helper { public: int operator()(int) const; private: std::map<int, int> memo; std::function<int(int)> fn; };
You can think of lambda mutable as not const operator() , in your case lambda can be defined as follows:
std::function<int(int)> helper = [=](int pos) mutable // etc
user657267
source share