What is called when a block returns a value?

I recently stumbled upon this code which does not look legal for me (but gcc compiles it). I do not really like the design, because I want to name it:

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

Apparently, the last value of the operator is returned as the "value" of the expression, limited by the namespace.

Edit: Thanks for the answers to the guys. Turns out this is an extension to a simple C called statement statement.

+7
source share
5 answers

This is not a namespace ; it is a macro that returns a maximum of two values.
\ at the end of the instructions is used to add 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 extension to the gcc compiler .

Good reading:

Statement Expressions:
A compound statement is a sequence of statements enclosed in braces. In GNU C, a compound statement in parentheses can be displayed as an expression in the so-called Statement expression .

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

The value of an operator expression is the value of the last simple expression that appears throughout the construct. If the last statement is not an expression, then the construct is of type void and does not matter.

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

+12
source

This is called an operator expression and is a non-standard extension of GCC. It allows you to use the compound operator in the form of an expression with the value specified by the last expression in the compound instruction.

It is used here to avoid the problem that macros like functions can evaluate their arguments several times, giving unexpected behavior if these evaluations have side effects. The macro is carefully written to evaluate a and b exactly once.

In C ++ you don't have to do anything - use function templates instead:

 template <typename T> T max(T const & a, T const & b) { return a > b ? a : b; } 
+7
source

First of all, this is not Standard C ++, because typeof is an extension of C ++, GCC. The code uses another extension called the Statement extension.

Compile your code with the -pedantic option, it will not compile.

As for the question, this is not a namespace. This is just a macro that gives you a maximum of two values.

+2
source

The {} operators in this context are the "anonymous scope operator" (also called the "lexical application", the "form", etc. They are used somewhat similar to the namespace to limit the scope of _a and _b located inside curly braces, therefore, they will not conflict with other vars that may have the same name. "auto" vars defined in {braces} will be "destroyed" after reaching the end of the curly brace ;, with non-local hyphenation, for example, "return" or "longjmp" However, you cannot use "goto" reliably. hold them out.

You are probably used to seeing them only after the "if", "do", "while" and "for" statements, but think of it as a way to "combine" several statements into one "slot", "just like you could would run multiple statements as a "then" or "else" clause for "if" (where, if you leave curly braces, you only have one slot ")"

As Mike Seymour noted, operation ({}) is a non-standard GCC extension that returns the value of the last element evaluated in it. It is very similar to the general survey operator, with the exception of an integral return at the end.

+2
source

This macro, like any other #DEFINE. In fact, the compiler replaces MAX (a, b) with the code defined in it. This will return the maximum value.

+1
source

All Articles