Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

c++ - What is it called when a block returns a value?

I came across this code recently, which doesn't look legal to me (but gcc compiles it). I don't so much mind the construction as want a name for it:

#define MAX(a,b) 
({ 
    typeof(a) _a = (a); 
    typeof(b) _b = (b); 
    (_a > _b) ? (_a) : (_b); 
})

Apparently, the last statement's value is being returned as the "value" of the expression bounded by the namespace.

Edit: Thanks for the answers guys. Turns out this is an extension to plain C called Statement Expressions.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is not a namespace, it is a macro which returns maximum of two values.
at the end of the statements is use to append multiple statements and create a multi-line macro.

The code is not standard C++ but it compiles in gcc because it is supported as an gcc compiler extension.

Good Read:

Statement Expressions:
A compound statement is a sequence of statements enclosed by braces. In GNU C, a compound statement inside parentheses may appear as an expression in what is called a Statement expression.

         .--------------.
         V              |
>>-(--{----statement--;-+--}--)--------------------------------><

The value of a statement expression is the value of the last simple expression to appear in the entire construct. If the last statement is not an expression, then the construct is of type void and has no value.

Note: This excerpt is taken from IBM XL C/C++ v7.0 documentation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...