#define FOO(val) \
({ \
int b = val; \
printf("First, let's count to %d\n", val); \
for (int i = 1; i <= val; i++) { \
printf("%d...\n", i); \
} \
b++; \
b; \
})
int main() {
int a = FOO(6);
printf("a=%d\n", a);
a = (4); // straightforward, everyone should be familiar with this
a = (4;); // does not compile
a = { 4; }; // does not compile
a = ({ 4;}); // valid, a will equal 4
return 0;
}
I'm trying to wrap my head around the above example. How and why does the FOO macro work (the last statement appears to act as a return value)? As far as syntax goes, what are the individual parts called and what other rules apply?
Some common synonyms of evaluate are appraise, assess, estimate, rate, and value. While all these words mean "to judge something with respect to its worth or significance," evaluate suggests an attempt to determine relative or intrinsic worth in terms other than monetary.
The EVALUATE statement operates as if each subject and object were evaluated and assigned a value or range of values. These values may be numeric, nonnumeric, truth values, or ranges of numeric or nonnumeric values.
verb (used with object), e·val·u·at·ed, e·val·u·at·ing. to determine or set the value or amount of; appraise: to evaluate property. to judge or determine the significance, worth, or quality of; assess: to evaluate the results of an experiment.
The main types of evaluation are process, impact, outcome and summative evaluation.
That's using a GCC non-standard extension to allow return values from code blocks, mostly intended for macros. The last value inside a code block gets treated as a "return value".
A clear example of why this was needed is the usual max
macro:
#define max(a,b) (a)<(b)?(b):(a)
Calling max(new A(), new B())
causes 3 objects to be allocated, when in fact you only want 2 (contrived example, but the point is one of the operands gets evaluated twice).
With this extension you can instead write:
#define max(a,b) ({ typeof(a) _a=(a); typeof(b) _b=(b); _a<_b?_b:_a; })
In this case both operands get evaluated exactly once, just like a function, but with all benefits of macros (such as they are).
This is called a statement expression, and is not a part of standard ISO, however GCC supports it.
Quoting from the manual:
A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.
...
The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct.
Its a GCC extension called statement expressions.
Whenever you see such weird syntax, must try compiling it with -pedantic
option as well. You will immediately know, if its supported by the language specification or not.
For example, I compiled it with -pedantic
option, and it says,
warning: ISO C++ forbids braced-groups within expressions
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