Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ^{ <stmts..> }() mean in C?

enter image description here

While reading one of LLVM static analyzer documents, I stumbled upon a strange operator.

^{ int y = x; }();

I know I can define a nested block inside a function like { ... }, but can we even call it? Also, I've never seen any usage placing ^ in front of a curly bracket block. I thought this is kind of a language extension supported by GCC and googled this with keywords like anonymous function or lambda but was of no avail. Is there anyone who has any idea about this?

like image 390
Gwangmu Lee Avatar asked Feb 07 '19 08:02

Gwangmu Lee


1 Answers

From Clang 9 Documentation Language Specification for Blocks it's a Block Literal Expression. It has the form of (from wiki):

^return_type ( parameters ) { function_body }

But:

If the return type is omitted and the argument list is ( void ), the ( void ) argument list may also be omitted.

The following:

^{ int y = x; }();

is equal to:

( ^void (void) { int y = x; } )();

is equal to:

void (^f)(void) = ^void (void) { int y = x; };
f();

It declares a block literal that does int y = x and immediately after declaring executes is.

like image 182
KamilCuk Avatar answered Nov 02 '22 17:11

KamilCuk