Preprocessor macro using caret ^ symbol at the start of an expression

烈酒焚心 提交于 2021-02-04 14:58:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!