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.
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;
}();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With