Lambda Captures by-value by-reference mutable
Choose capture clauses thoughtfully and understand lambda state.
Captures Recap
A lambda s capture clause lists variables from the surrounding scope it can use. The capture mode controls how each variable is accessed.
Capture by Value
Names listed without & are copied at lambda creation. The lambda owns its copy.
int n = 5;
auto f = [n]() { return n * 2; };
n = 10;
std::cout << f(); // 10 (captured n was 5)All lessons in this course
- Function Objects Functors
- Lambda Captures by-value by-reference mutable
- Generic Lambdas and Closures C++14
- std::function Type-erased Callables