What are lambda expressions, and how does 'capturing' work (by value vs. by reference)?
Last updated
A lambda is an inline anonymous function object (
[capture](params) { body }). Its capture clause decides which surrounding variables it can use.By value (
[=],[x]) copies the variable at capture time and is safe to outlive it. Copies are const unless the lambda ismutable.By reference (
[&],[&x]) sees live changes but dangles if the lambda outlives the variable. Prefer by-value for lambdas that escape their scope.