I've noticed that gcc
accepts code like:
if ( ({ int ret; /* code here */; ret }) == some_value)
...;
for ( i = ({ int ret; /* code here */; ret }); i < top; i++)
...;
and so on.
One can create an anonymous codeblock and treat it as rvalue (as long as the last statement is a variable/value).
This is useful in some situations - the particular use-case I've found is to return values from inline assembly statements this way. But it'd also be possible to use this to convert functions returning void
but modifying memory through pointer arguments, like:
void myfunc(int *arg) { *arg = ...; }
if ( ({int ret; myfunc(&ret); ret; }) == 54321) { ... };
I've been trying to find documentation that this is *normal8 / that the C standard allows for this kind of thing, poor man's lambda.
Can anyone provide details ?