1<html lang="en"> 2<head> 3<title>Statement Exprs - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions"> 9<link rel="next" href="Local-Labels.html#Local-Labels" title="Local Labels"> 10<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 11<!-- 12Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 131998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 142010 Free Software Foundation, Inc. 15 16Permission is granted to copy, distribute and/or modify this document 17under the terms of the GNU Free Documentation License, Version 1.3 or 18any later version published by the Free Software Foundation; with the 19Invariant Sections being ``Funding Free Software'', the Front-Cover 20Texts being (a) (see below), and with the Back-Cover Texts being (b) 21(see below). A copy of the license is included in the section entitled 22``GNU Free Documentation License''. 23 24(a) The FSF's Front-Cover Text is: 25 26 A GNU Manual 27 28(b) The FSF's Back-Cover Text is: 29 30 You have freedom to copy and modify this GNU Manual, like GNU 31 software. Copies published by the Free Software Foundation raise 32 funds for GNU development.--> 33<meta http-equiv="Content-Style-Type" content="text/css"> 34<style type="text/css"><!-- 35 pre.display { font-family:inherit } 36 pre.format { font-family:inherit } 37 pre.smalldisplay { font-family:inherit; font-size:smaller } 38 pre.smallformat { font-family:inherit; font-size:smaller } 39 pre.smallexample { font-size:smaller } 40 pre.smalllisp { font-size:smaller } 41 span.sc { font-variant:small-caps } 42 span.roman { font-family:serif; font-weight:normal; } 43 span.sansserif { font-family:sans-serif; font-weight:normal; } 44--></style> 45<link rel="stylesheet" type="text/css" href="../cs.css"> 46</head> 47<body> 48<div class="node"> 49<a name="Statement-Exprs"></a> 50<p> 51Next: <a rel="next" accesskey="n" href="Local-Labels.html#Local-Labels">Local Labels</a>, 52Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 53<hr> 54</div> 55 56<h3 class="section">6.1 Statements and Declarations in Expressions</h3> 57 58<p><a name="index-statements-inside-expressions-2239"></a><a name="index-declarations-inside-expressions-2240"></a><a name="index-expressions-containing-statements-2241"></a><a name="index-macros_002c-statements-in-expressions-2242"></a> 59<!-- the above section title wrapped and causes an underfull hbox.. i --> 60<!-- changed it from "within" to "in". -mew 4feb93 --> 61A compound statement enclosed in parentheses may appear as an expression 62in GNU C. This allows you to use loops, switches, and local variables 63within an expression. 64 65 <p>Recall that a compound statement is a sequence of statements surrounded 66by braces; in this construct, parentheses go around the braces. For 67example: 68 69<pre class="smallexample"> ({ int y = foo (); int z; 70 if (y > 0) z = y; 71 else z = - y; 72 z; }) 73</pre> 74 <p class="noindent">is a valid (though slightly more complex than necessary) expression 75for the absolute value of <code>foo ()</code>. 76 77 <p>The last thing in the compound statement should be an expression 78followed by a semicolon; the value of this subexpression serves as the 79value of the entire construct. (If you use some other kind of statement 80last within the braces, the construct has type <code>void</code>, and thus 81effectively no value.) 82 83 <p>This feature is especially useful in making macro definitions “safe” (so 84that they evaluate each operand exactly once). For example, the 85“maximum” function is commonly defined as a macro in standard C as 86follows: 87 88<pre class="smallexample"> #define max(a,b) ((a) > (b) ? (a) : (b)) 89</pre> 90 <p class="noindent"><a name="index-side-effects_002c-macro-argument-2243"></a>But this definition computes either <var>a</var> or <var>b</var> twice, with bad 91results if the operand has side effects. In GNU C, if you know the 92type of the operands (here taken as <code>int</code>), you can define 93the macro safely as follows: 94 95<pre class="smallexample"> #define maxint(a,b) \ 96 ({int _a = (a), _b = (b); _a > _b ? _a : _b; }) 97</pre> 98 <p>Embedded statements are not allowed in constant expressions, such as 99the value of an enumeration constant, the width of a bit-field, or 100the initial value of a static variable. 101 102 <p>If you don't know the type of the operand, you can still do this, but you 103must use <code>typeof</code> (see <a href="Typeof.html#Typeof">Typeof</a>). 104 105 <p>In G++, the result value of a statement expression undergoes array and 106function pointer decay, and is returned by value to the enclosing 107expression. For instance, if <code>A</code> is a class, then 108 109<pre class="smallexample"> A a; 110 111 ({a;}).Foo () 112</pre> 113 <p class="noindent">will construct a temporary <code>A</code> object to hold the result of the 114statement expression, and that will be used to invoke <code>Foo</code>. 115Therefore the <code>this</code> pointer observed by <code>Foo</code> will not be the 116address of <code>a</code>. 117 118 <p>Any temporaries created within a statement within a statement expression 119will be destroyed at the statement's end. This makes statement 120expressions inside macros slightly different from function calls. In 121the latter case temporaries introduced during argument evaluation will 122be destroyed at the end of the statement that includes the function 123call. In the statement expression case they will be destroyed during 124the statement expression. For instance, 125 126<pre class="smallexample"> #define macro(a) ({__typeof__(a) b = (a); b + 3; }) 127 template<typename T> T function(T a) { T b = a; return b + 3; } 128 129 void foo () 130 { 131 macro (X ()); 132 function (X ()); 133 } 134</pre> 135 <p class="noindent">will have different places where temporaries are destroyed. For the 136<code>macro</code> case, the temporary <code>X</code> will be destroyed just after 137the initialization of <code>b</code>. In the <code>function</code> case that 138temporary will be destroyed when the function returns. 139 140 <p>These considerations mean that it is probably a bad idea to use 141statement-expressions of this form in header files that are designed to 142work with C++. (Note that some versions of the GNU C Library contained 143header files using statement-expression that lead to precisely this 144bug.) 145 146 <p>Jumping into a statement expression with <code>goto</code> or using a 147<code>switch</code> statement outside the statement expression with a 148<code>case</code> or <code>default</code> label inside the statement expression is 149not permitted. Jumping into a statement expression with a computed 150<code>goto</code> (see <a href="Labels-as-Values.html#Labels-as-Values">Labels as Values</a>) yields undefined behavior. 151Jumping out of a statement expression is permitted, but if the 152statement expression is part of a larger expression then it is 153unspecified which other subexpressions of that expression have been 154evaluated except where the language definition requires certain 155subexpressions to be evaluated before or after the statement 156expression. In any case, as with a function call the evaluation of a 157statement expression is not interleaved with the evaluation of other 158parts of the containing expression. For example, 159 160<pre class="smallexample"> foo (), (({ bar1 (); goto a; 0; }) + bar2 ()), baz(); 161</pre> 162 <p class="noindent">will call <code>foo</code> and <code>bar1</code> and will not call <code>baz</code> but 163may or may not call <code>bar2</code>. If <code>bar2</code> is called, it will be 164called after <code>foo</code> and before <code>bar1</code> 165 166 </body></html> 167 168