[cfe-dev] Blocks vs. C++ lambda's?

Remy Demarest psycho.hedgehog at me.com
Thu Apr 30 08:31:04 PDT 2009


Hi,

The non-serious answer would be: because the C++(0x) syntax sucks. :P

And now the serious answer: from what I can tell, C++(0x) lambdas  
cannot be used outside of their declaration scope at least if it uses  
variables that are contained in the innermost block scope of its  
creation...

Whereas Apple's blocks have a few advantages, first they have a simple  
syntax, their type declaration is very close from function pointers  
(you replace * by ^), second, you don't need to declare which  
variables from the enclosing scope you want to use inside your block...
All of them can be used inside, in case you want to modify one of the  
captured variables, you need to add the __block type specifier in  
front of the variable declarations. This is the equivalent to the  
[&total]{ total += 5; } syntax that is used in C++'s lambdas, the  
reference & operator doesn't mean the same thing in C, and we need the  
blocks to be compatible with C.

And most importantly, the best reason to prefer Apple's feature is  
that you can fully use the blocks outside of their declaration scope,  
even though the variables (__block or not) from the enclosing scope  
are no more on the current stack frame. This is done by copying the  
block to the heap along with all the variables it uses, and if two  
blocks uses/modifies the same __block variable in the declaration  
scope, both blocks will see the same changes, even when copied on the  
heap.

Other reasons to prefer Apple's feature is that it's fully available  
in C, C++ and ObjC altogether, it supports the garbage collection of  
ObjC with __weak __block variables, it has additions for ObjC and C++  
types, and also, it's a first class object in ObjC and can thus  
receive messages.

Remy Demarest

Le 30 avr. 2009 à 14:36, Boris Dušek a écrit :

> Hi,
>
> I learned about the blocks feature Apple is implementing as an  
> extension to C. After looking at it briefly, I concluded it's  
> basically the same thing as C++(0x) lambdas. But there should be a  
> difference since if not, (I guess) it would make sense to just adopt  
> the C++ lambda syntax in C and Obj-C to achieve the goal. So could  
> someone please point out what blocks provide that C++ lambdas do not  
> (and vice versa)?
>
> Thanks,
> Boris Dušek
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev





More information about the cfe-dev mailing list