So, I just upgraded to Xcode 4.4, and I noticed in the changelog:
Apple LLVM compiler supports additional C++11 features, including lambdas
Which is awesome! So I got around to coding, and I found a few things out:
-
Lambdas are assignable to Objective-C blocks:
void (^block)() = []() -> void { NSLog(@"Inside Lambda called as block!"); }; block(); -
std::functioncan hold an Objective-C block:std::function<void(void)> func = ^{ NSLog(@"Block inside std::function"); }; func(); -
We cant assign an Objective-C block to a lambda:
auto lambda = []() -> { NSLog(@"Lambda!"); }; lambda = ^{ // error! NSLog(@"Block!"); }; lambda();
Why is this? Shouldn’t the two be semantically equivalent, given what we’ve seen above?
C++11’s lambda’s copy-assignment operator is explicitly disabled1. This is not a matter of “semantically equivalent”. It can’t even assign back to itself. Not to mention an unrelated type.
std::functioncan hold an Objective-C block.std::functioncan hold any types which can be invoked asf(a,b,c,...). Since blocks support “the invoke operator”, it can also be held by astd::function. But notice that Objective-C and C++ follow different memory management scheme, so storing a block in astd::functionfor a long time may cause dangling reference.Lambdas are assignable to Objective-C blocks:
1: C++11 §5.1.2/19:
2: http://llvm.org/viewvc/llvm-project?view=rev&revision=150620