Looking at this page: http://www.mikeash.com/pyblog/friday-qa-2010-12-31-c-macro-tips-and-tricks.html
I found this snippet of code with ^{
... }()
syntax, what are the caret/brackets doing?
#define MAX(x, y) (^{ \
int my_localx = (x); \
int my_localy = (y); \
return my_localx > my_localy ? (my_localx) : (my_localy); \
}())
It looks like its creating an anonymous function or something. What is this concept called? Where can I read about it?
It's a C block. It's quite like an anonymous function (in use, not in structure). You can read more about them on Mike Ash's site and in Apple's documentation.
It's a block. It's not standard C, but it is supported by Apple's LLVM compiler (around about Xcode 3.2 IIRC and later). See here and here for more details.
It's not just for Objective-C, but is part of the C and C++ compilers also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With