This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: apple blocks extension



On Sep 15, 2009, at 9:04 AM, Richard Henderson wrote:


On 09/15/2009 08:28 AM, Vincent R. wrote:
I just was curious to know if closures in apple gcc(called blocks from
what I read) is
also in mainline.
What is the status about this extension ?

It is unlikely that this will ever be brought into GCC, since it appears to be largely identical to the C++0x Lambda feature.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1968.pdf

There are major differences between lambdas and blocks, though they serve a superficially similar purpose. If you are interested, there is a ton of discussion online. Here are the two biggest differences:



The first difference is that every instance of a lambda gives you a value of a new (anonymous) type, which makes them mostly only useful with templates. Blocks literals give values of a predictable type, which means that you can pass them around to non-templates. This is also why they "work" in C. Yes, I know that there are various adaptor classes that can be used with C++ lambdas to help work around this, but this is in inherent part of blocks.



The second major feature of Blocks vs c++ lambdas is that they can be "copied onto the heap". This allows things like "Grand Central Dispatch" to work: you can write code that executes blocks asynchronously or on other threads/work queues (after the function containing the block has returned). A simple example is:


void print_on_different_thread(int X) {
  run_asynch(^{
      printf("Hi %d\n", X);
  });
}

With lambdas, the closure would be go out of scope when print_on_different_thread returns, but blocks allows "run_asynch" to extend the lifetime of the block.

Blocks and Lambdas have intentionally different syntax, allowing them to coexist peacefully in the same compiler / language. They are also superficially similar to nested functions, but have other sets of benefits. For example, blocks don't require executable stacks etc.

-Chris


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]