Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++0x lambda vs blocks

I was exploring C++0x today, and I encountered the new lambda feature. My question is how are these different (in terms of use) from blocks and why might one prefer one over the other?

Thanks.

like image 762
Maz Avatar asked Aug 15 '11 22:08

Maz


2 Answers

there is a a short syntax with C++0x lambdas to take every variable in scope by reference. ([&]) The type of a lambda is also unspecified, allowing potentially more optimal code.

Now, when you look at Apple blocks, it will require __block specifiers added to variables you want to modify (the very fact that this is required suggests the whole system is defective). Variables are taken by reference but then by value when the block exits the scope (and the copied context necessarily lives on the heap, it seems). A weird semantic that will only lead to broken designs, but probably makes people that love GC happy. Without saying this probably has quite the efficiency cost, of course, since this requires special indirections.

It is claimed the C++0x lambdas syntax would break compatibility with C programs, but I don't think that is true. There are probably other problems to integrate it with C, though, mainly the fact that C can't really deal with unspecified types and build type erasure.

Apple blocks is really just an ObjC feature they try to generalize to other languages. For C++, the system designed for that language is just so much better.

EDIT:

To properly give credit, I took this information from http://www.rhinocerus.net/forum/language-c-moderated/558214-blocks-vs-c-lambdas.html a long time ago. That link is dead now; however, the original discussion appears to be archived here, thanks to @stefan for finding it.

like image 65
djhaskin987 Avatar answered Oct 02 '22 20:10

djhaskin987


I think it basically comes down to a question of your starting point. If you're starting from Objective-C, and writing C++ (Objective-C++) primarily (or exclusively) as an adjunct to Objective-C, then using blocks throughout all the code may make sense, simply to retain as much commonality as possible across the code base. Even if (for example) a project used some pieces written in Objective-C and others in C++, it could make sense to use blocks in both retain as much similarity throughout the code base as possible.

Unless you're using them outside of C++, however, I see little reason to prefer blocks over C++ lambdas. In what I'd guess to be the most common use (a predicate or action in an algorithm) the only noticeable difference between the two would be that one starts with ^ and the other with [].

Older versions of Objective C++

Before the ARC, there were internal differences in the implementation of blocks and lambdas that were likely to affect some more advanced uses. For example, blocks worked vaguely like C strings, so you used Block_copy to copy one, Block_release to free the copy, and so on. On the other hand, in C++ this is all automated so the copy ctor automatically uses Block_copy and the dtor Block_release as needed. At the same time, it did involve a bit more "magic", so (for example) when you copy a block, the copy is always allocated dynamically, regardless of how the source was allocated.

If, for one reason or another, you're stuck with using an older (I'm tempted to say "ancient") compiler or maintaining older code (and don't want to update the codebase as a whole) the memory management difference may be worth taking into account.

like image 42
Jerry Coffin Avatar answered Oct 02 '22 19:10

Jerry Coffin