Objective-C++ 11 - Why can't we assign a block to a lambda?

Viewed 4072

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:

  1. Lambdas are assignable to Objective-C blocks:

    void (^block)() = []() -> void { 
        NSLog(@"Inside Lambda called as block!");
    };
    
    block();
    
  2. std::function can hold an Objective-C block:

    std::function<void(void)> func = ^{
        NSLog(@"Block inside std::function");
    };
    
    func();
    
  3. 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?

2 Answers
Related