Can I declare ObjC block with auto?
auto fun = ^(int x) { NSLog(@"%d", x); }
fun(5);
I cannot work out valid syntax for that.
Can I declare ObjC block with auto?
auto fun = ^(int x) { NSLog(@"%d", x); }
fun(5);
I cannot work out valid syntax for that.
You are missing a ;
after the declaration of fun
. Otherwise, you got the syntax right, and Clang will accept that in -std=c++11 -fblocks
mode, for C++ or Objective-C++ input. (Note that blocks are actually an orthogonal extension which is not part of Objective-C.)
I don't think the auto keyword from C++/Objective-C++ is used in objective-C.
As for declaring block variable for your example the following will work in objective-C
void(^fun)(int x) = ^(int x) {
NSLog(@"%d",x);
};
fun(5);
For more declaration options on block there's a very good answer here
The auto keyword is a c++11 keyword. Objective-c is a superset of c not c++ and therefor does not contain the properties of c++, but rather c. As for objective-c++, I do not believe that clang is up to date on all of the new c++11 features, especially in the compiler that builds objective-c++. Hope this helps!