Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ({}) called in C++?

I just read code like this:

auto value = ({
  auto it = container.find(key);
  it != container.end() ? it->second : default_value;
});

What is this ({}) called? I don't think I've ever seen this before.

like image 295
Hesky Fisher Avatar asked Nov 16 '25 02:11

Hesky Fisher


1 Answers

It is a gcc extension, so not standard C++,

it is statement expression.

Since C++11, you might use Immediately Invoked Function Expressions (IIFE) with lambda instead in most cases:

auto value = [&](){
  auto it = container.find(key);
  return it != container.end() ? it->second : default_value;
}();
like image 182
Jarod42 Avatar answered Nov 18 '25 17:11

Jarod42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!