问题
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?
回答1:
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.
回答2:
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.
回答3:
Official Apple Documentation on C Blocks
回答4:
It is a C block, which will create an anonimous function.
Note that it will create calls to the system API to handle those calls, don't know about mac, but for iOS, the runtime system must be 3.2 or later to support those calls.
来源:https://stackoverflow.com/questions/6852291/preprocessor-macro-using-caret-symbol-at-the-start-of-an-expression