[cfe-dev] blocks and lambdas

James Gregurich bayoubengal at mac.com
Mon Feb 7 13:11:19 PST 2011

I suppose the following points are the critical points according to my current understanding:

1)   integration of lambdas with libdispatch.  As a 3rd party commercial developer, I need to be able to write standard code that works across platforms cleanly and efficiently....that means using C++0x Lambda functions rather than blocks. Microsoft has set their parallel APIs up to use lambda functions. Being able to submit lambda functions to the normal dispatch queues is critically important. If I get that ability, I can live with it.

2) memory management. I don't know all the details on the differences, but I what I have read about the differences focuses on memory management of stack variables. You can tell "blocks" to preserve the lifetime of stack variables if they are passed off to blocks that survive the scope of the current function. What I would submit is that for C++ code, if one wants to preserve such variables, he'll be using shared_ptr or some other reference counted smart-pointer. So, it seems to me that you should be able to map a lambda to a block...copying the required stack variables by value and if the programmer does something stupid like pass a reference to a stack variable, then the app will crash and he'll know about it pretty quickly. Given standard practice in C++-land, I wouldn't worry about the __blocks keyword and such with lambdas. The C++ programmer is inherently expected to understand the lifetime of the variables he is passing to the lambda. He will implicitly determine the lifetimes of his variables by how be declares them (shared_ptr, file scope, static, etc) . So, if you don't worry about the __blocks keyword, it seems to me that you should be able to map a lambda into the existing blocks runtime mechanism in a straight-forward way. The reason this feature would be useful is that one could write even his mac-specific cocoa code in terms of lambda functions to keep his codebase more consistent....having everything in lambda syntax rather than mixing the two syntaxes. Using the lambda syntax may indeed allow for additional sharing of code across platforms.

I admit that I am not fully informed on the subject and I'm throwing my thoughts out there for the sake of discussion. If there is some point I'm missing, please feel free to point me to an explanation of it. I'd appreciate the education.


On Feb 7, 2011, at 12:26 PM, Chris Lattner wrote:

> On Feb 7, 2011, at 11:18 AM, James Gregurich wrote:
>> hi,
>> What is to be the relationship between Apple's blocks standard and C++0x lambda functions? Are these things features going to be integrated or interchangeable in clang?
> They provide similar functionality, but have many differences.  Clang will implement both as independent language constructs.  Library code that wants to use both should be able to handle them through std::function (for example).
> -Chris

More information about the cfe-dev mailing list