122
In addition, there are semantic issues with statement-expressions in C++. If you try to use statement-
expressions instead of inline functions in C++, you may be surprised at the way object destruction is
handled. For example:
#define foo(a)
does not work the same way as:
inline int foo(int a) { int b = a; return b + 3; }
In particular, if the expression passed into
for those temporaries will be run earlier in the case of the macro than in the case of the function.
These considerations mean that it is probably a bad idea to use statement-expressions of this form
in header files that are designed to work with C++. (Note that some versions of the GNU C Library
contained header files using statement-expression that lead to precisely this bug.)
6.2. Locally Declared Labels
Each statement expression is a scope in which local labels can be declared. A local label is simply
an identifier; you can jump to it with an ordinary
expression it belongs to.
A local label declaration looks like this:
__label__
;
label
or
__label__
label1
Local label declarations must come at the beginning of the statement expression, right after the
before any ordinary declarations.
The label declaration defines the label name, but does not define the label itself. You must do this in
the usual way, with
label
The local label feature is useful because statement expressions are often used in macros. If the macro
contains nested loops, a
whose scope is the whole function cannot be used: if the macro can be expanded several times in one
function, the label will be multiply defined in that function. A local label avoids this problem. For
example:
#define SEARCH(array, target)
({
__label__ found;
typeof (target) _SEARCH_target = (target);
typeof (*(array)) *_SEARCH_array = (array);
int i, j;
int value;
for (i = 0; i
for (j = 0; j
if (_SEARCH_array[i][j] == _SEARCH_target)
{ value = i; goto found; }
value = -1;
found:
value;
})
({int b = (a); b + 3; })
,
, /* ... */;
label2
, within the statements of the statement expression.
:
can be useful for breaking out of them. However, an ordinary label
goto
max; i++)
max; j++)
Chapter 6. Extensions to the C Language Family
involves the creation of temporaries, the destructors
foo
statement, but only from within the statement
goto
\
\
\
\
\
\
\
\
\
\
\
\
\
\
,
({
Need help?
Do you have a question about the ENTERPRISE LINUX 3 - USING GCC and is the answer not in the manual?
Questions and answers