开发者

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?


Such arguments are a red herring. Yes, lambdas have memory management issues to deal with, but a lambda is basically like a function object (functor) with member variables. Whatever issues a functor has to deal with, a lambda has to deal with too. C++0x lambdas have facilities to decide which objects to capture and whether it should be by value or by reference. This is analogous to storing values and references in a functor object.


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.


How do the Lambda Expressions / Closures in C++0x complicate the memory management in C++?

Do they? They're just syntactic sugar for creating function objects. Nothing we havn't seen before. Only now, we have a short syntax for creating those on-the-fly.

Why do some people say that closures have no place in languages with manual memory management?

You should probably ask them. I find the C++0x lambda approach rather elegant and low-level. It fits the spirit of C++.

Is there claim valid and if yes, what are the reasons behind it?

Beats me. Care to dig up some arguments yourself?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜