mutable lambdas

suggest change

By default, the implicit operator() of a lambda is const. This disallows performing non-const operations on the lambda. In order to allow modifying members, a lambda may be marked mutable, which makes the implicit operator() non-const:

int a = 0;

auto bad_counter = [a] {
    return a++;   // error: operator() is const
                  // cannot modify members

auto good_counter = [a]() mutable {
    return a++;  // OK

good_counter(); // 0
good_counter(); // 1
good_counter(); // 2

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents