Mutating Lambda Member Variables

Understand mutating lambda member variables, their behavior from the compiler's perspective and the use of capture.

Using the mutable keyword in lambdas

As the lambda works just like a class with member variables, it can also mutate them. However, the function call operator of a lambda is const by default, so we explicitly need to specify that the lambda can mutate its members by using the mutable keyword. In the following example, the lambda mutates the counter variable every time it's invoked:

Get hands-on with 1200+ tech skills courses.