Lambdas are not fully equivalent since return statements in statement expressions will return at the function level, whereas return statements in lambdas will only return at the lambda level.
Just for clarity, since I didn't understand on first reading.
int foo() {
int result = ({
if (some_condition)
return -1; // This returns from foo(), not just the statement expression
42;
});
// This line might never be reached
}
There is no C equivalent, but in in C++, since C++11 you can achieve the same effect with lambdas:
As noted in the linked SO discussion, this is analogous to a JS Immediately-Invoked Function Expression (IIFE).[1] https://stackoverflow.com/questions/76890861/what-is-called-...