You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
211 lines
9.1 KiB
HTML
211 lines
9.1 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<!-- Copyright (C) 1988-2018 Free Software Foundation, Inc.
|
|
|
|
Permission is granted to copy, distribute and/or modify this document
|
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
|
any later version published by the Free Software Foundation; with the
|
|
Invariant Sections being "Funding Free Software", the Front-Cover
|
|
Texts being (a) (see below), and with the Back-Cover Texts being (b)
|
|
(see below). A copy of the license is included in the section entitled
|
|
"GNU Free Documentation License".
|
|
|
|
(a) The FSF's Front-Cover Text is:
|
|
|
|
A GNU Manual
|
|
|
|
(b) The FSF's Back-Cover Text is:
|
|
|
|
You have freedom to copy and modify this GNU Manual, like GNU
|
|
software. Copies published by the Free Software Foundation raise
|
|
funds for GNU development. -->
|
|
<!-- Created by GNU Texinfo 6.4, http://www.gnu.org/software/texinfo/ -->
|
|
<head>
|
|
<title>Statement Exprs (Using the GNU Compiler Collection (GCC))</title>
|
|
|
|
<meta name="description" content="Statement Exprs (Using the GNU Compiler Collection (GCC))">
|
|
<meta name="keywords" content="Statement Exprs (Using the GNU Compiler Collection (GCC))">
|
|
<meta name="resource-type" content="document">
|
|
<meta name="distribution" content="global">
|
|
<meta name="Generator" content="makeinfo">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<link href="index.html#Top" rel="start" title="Top">
|
|
<link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
|
|
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
|
<link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
|
|
<link href="Local-Labels.html#Local-Labels" rel="next" title="Local Labels">
|
|
<link href="C-Extensions.html#C-Extensions" rel="prev" title="C Extensions">
|
|
<style type="text/css">
|
|
<!--
|
|
a.summary-letter {text-decoration: none}
|
|
blockquote.indentedblock {margin-right: 0em}
|
|
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
|
|
blockquote.smallquotation {font-size: smaller}
|
|
div.display {margin-left: 3.2em}
|
|
div.example {margin-left: 3.2em}
|
|
div.lisp {margin-left: 3.2em}
|
|
div.smalldisplay {margin-left: 3.2em}
|
|
div.smallexample {margin-left: 3.2em}
|
|
div.smalllisp {margin-left: 3.2em}
|
|
kbd {font-style: oblique}
|
|
pre.display {font-family: inherit}
|
|
pre.format {font-family: inherit}
|
|
pre.menu-comment {font-family: serif}
|
|
pre.menu-preformatted {font-family: serif}
|
|
pre.smalldisplay {font-family: inherit; font-size: smaller}
|
|
pre.smallexample {font-size: smaller}
|
|
pre.smallformat {font-family: inherit; font-size: smaller}
|
|
pre.smalllisp {font-size: smaller}
|
|
span.nolinebreak {white-space: nowrap}
|
|
span.roman {font-family: initial; font-weight: normal}
|
|
span.sansserif {font-family: sans-serif; font-weight: normal}
|
|
ul.no-bullet {list-style: none}
|
|
-->
|
|
</style>
|
|
|
|
|
|
</head>
|
|
|
|
<body lang="en">
|
|
<a name="Statement-Exprs"></a>
|
|
<div class="header">
|
|
<p>
|
|
Next: <a href="Local-Labels.html#Local-Labels" accesskey="n" rel="next">Local Labels</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<hr>
|
|
<a name="Statements-and-Declarations-in-Expressions"></a>
|
|
<h3 class="section">6.1 Statements and Declarations in Expressions</h3>
|
|
<a name="index-statements-inside-expressions"></a>
|
|
<a name="index-declarations-inside-expressions"></a>
|
|
<a name="index-expressions-containing-statements"></a>
|
|
<a name="index-macros_002c-statements-in-expressions"></a>
|
|
|
|
<p>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.
|
|
</p>
|
|
<p>Recall that a compound statement is a sequence of statements surrounded
|
|
by braces; in this construct, parentheses go around the braces. For
|
|
example:
|
|
</p>
|
|
<div class="smallexample">
|
|
<pre class="smallexample">({ int y = foo (); int z;
|
|
if (y > 0) z = y;
|
|
else z = - y;
|
|
z; })
|
|
</pre></div>
|
|
|
|
<p>is a valid (though slightly more complex than necessary) expression
|
|
for the absolute value of <code>foo ()</code>.
|
|
</p>
|
|
<p>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. (If you use some other kind of statement
|
|
last within the braces, the construct has type <code>void</code>, and thus
|
|
effectively no value.)
|
|
</p>
|
|
<p>This feature is especially useful in making macro definitions “safe” (so
|
|
that they evaluate each operand exactly once). For example, the
|
|
“maximum” function is commonly defined as a macro in standard C as
|
|
follows:
|
|
</p>
|
|
<div class="smallexample">
|
|
<pre class="smallexample">#define max(a,b) ((a) > (b) ? (a) : (b))
|
|
</pre></div>
|
|
|
|
<p><a name="index-side-effects_002c-macro-argument"></a>
|
|
But this definition computes either <var>a</var> or <var>b</var> twice, with bad
|
|
results if the operand has side effects. In GNU C, if you know the
|
|
type of the operands (here taken as <code>int</code>), you can define
|
|
the macro safely as follows:
|
|
</p>
|
|
<div class="smallexample">
|
|
<pre class="smallexample">#define maxint(a,b) \
|
|
({int _a = (a), _b = (b); _a > _b ? _a : _b; })
|
|
</pre></div>
|
|
|
|
<p>Embedded statements are not allowed in constant expressions, such as
|
|
the value of an enumeration constant, the width of a bit-field, or
|
|
the initial value of a static variable.
|
|
</p>
|
|
<p>If you don’t know the type of the operand, you can still do this, but you
|
|
must use <code>typeof</code> or <code>__auto_type</code> (see <a href="Typeof.html#Typeof">Typeof</a>).
|
|
</p>
|
|
<p>In G++, the result value of a statement expression undergoes array and
|
|
function pointer decay, and is returned by value to the enclosing
|
|
expression. For instance, if <code>A</code> is a class, then
|
|
</p>
|
|
<div class="smallexample">
|
|
<pre class="smallexample"> A a;
|
|
|
|
({a;}).Foo ()
|
|
</pre></div>
|
|
|
|
<p>constructs a temporary <code>A</code> object to hold the result of the
|
|
statement expression, and that is used to invoke <code>Foo</code>.
|
|
Therefore the <code>this</code> pointer observed by <code>Foo</code> is not the
|
|
address of <code>a</code>.
|
|
</p>
|
|
<p>In a statement expression, any temporaries created within a statement
|
|
are destroyed at that statement’s end. This makes statement
|
|
expressions inside macros slightly different from function calls. In
|
|
the latter case temporaries introduced during argument evaluation are
|
|
destroyed at the end of the statement that includes the function
|
|
call. In the statement expression case they are destroyed during
|
|
the statement expression. For instance,
|
|
</p>
|
|
<div class="smallexample">
|
|
<pre class="smallexample">#define macro(a) ({__typeof__(a) b = (a); b + 3; })
|
|
template<typename T> T function(T a) { T b = a; return b + 3; }
|
|
|
|
void foo ()
|
|
{
|
|
macro (X ());
|
|
function (X ());
|
|
}
|
|
</pre></div>
|
|
|
|
<p>has different places where temporaries are destroyed. For the
|
|
<code>macro</code> case, the temporary <code>X</code> is destroyed just after
|
|
the initialization of <code>b</code>. In the <code>function</code> case that
|
|
temporary is destroyed when the function returns.
|
|
</p>
|
|
<p>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 expressions that lead to precisely this
|
|
bug.)
|
|
</p>
|
|
<p>Jumping into a statement expression with <code>goto</code> or using a
|
|
<code>switch</code> statement outside the statement expression with a
|
|
<code>case</code> or <code>default</code> label inside the statement expression is
|
|
not permitted. Jumping into a statement expression with a computed
|
|
<code>goto</code> (see <a href="Labels-as-Values.html#Labels-as-Values">Labels as Values</a>) has undefined behavior.
|
|
Jumping out of a statement expression is permitted, but if the
|
|
statement expression is part of a larger expression then it is
|
|
unspecified which other subexpressions of that expression have been
|
|
evaluated except where the language definition requires certain
|
|
subexpressions to be evaluated before or after the statement
|
|
expression. In any case, as with a function call, the evaluation of a
|
|
statement expression is not interleaved with the evaluation of other
|
|
parts of the containing expression. For example,
|
|
</p>
|
|
<div class="smallexample">
|
|
<pre class="smallexample"> foo (), (({ bar1 (); goto a; 0; }) + bar2 ()), baz();
|
|
</pre></div>
|
|
|
|
<p>calls <code>foo</code> and <code>bar1</code> and does not call <code>baz</code> but
|
|
may or may not call <code>bar2</code>. If <code>bar2</code> is called, it is
|
|
called after <code>foo</code> and before <code>bar1</code>.
|
|
</p>
|
|
<hr>
|
|
<div class="header">
|
|
<p>
|
|
Next: <a href="Local-Labels.html#Local-Labels" accesskey="n" rel="next">Local Labels</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
|
|
|
|
|
|
</body>
|
|
</html>
|