Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 321
pyramation Avatar asked Jul 27 '11 23:07

pyramation


2 Answers

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.

like image 196
Cajunluke Avatar answered Sep 20 '22 17:09

Cajunluke


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.

like image 23
ldav1s Avatar answered Sep 20 '22 17:09

ldav1s