Search code examples
c++programming-languagesfunctional-programminglambdac++11

Lambda Expressions and Memory Management


How do the Lambda Expressions / Closures in C++0x complicate the memory management in C++? Why do some people say that closures have no place in languages with manual memory management? Is their claim valid and if yes, what are the reasons behind it?


Solution

  • Lambdas can outlive the context they were created in. Binding free variables by reference can be an issue then, because when the lambda wants to access them later, they may not exist anymore. It's simply "Don't return local variables by reference" in disguise.