Statement-Exprs.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Statement Exprs</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Statement Exprs">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Statement Exprs">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Local-Labels.html#Local-Labels" rel="next" title="Local Labels">
  31. <link href="C-Extensions.html#C-Extensions" rel="prev" title="C Extensions">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Statement-Exprs"></a>
  63. <div class="header">
  64. <p>
  65. 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> &nbsp; [<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>
  66. </div>
  67. <hr>
  68. <a name="Statements-and-Declarations-in-Expressions"></a>
  69. <h3 class="section">6.1 Statements and Declarations in Expressions</h3>
  70. <a name="index-statements-inside-expressions"></a>
  71. <a name="index-declarations-inside-expressions"></a>
  72. <a name="index-expressions-containing-statements"></a>
  73. <a name="index-macros_002c-statements-in-expressions"></a>
  74. <p>A compound statement enclosed in parentheses may appear as an expression
  75. in GNU C. This allows you to use loops, switches, and local variables
  76. within an expression.
  77. </p>
  78. <p>Recall that a compound statement is a sequence of statements surrounded
  79. by braces; in this construct, parentheses go around the braces. For
  80. example:
  81. </p>
  82. <div class="smallexample">
  83. <pre class="smallexample">({ int y = foo (); int z;
  84. if (y &gt; 0) z = y;
  85. else z = - y;
  86. z; })
  87. </pre></div>
  88. <p>is a valid (though slightly more complex than necessary) expression
  89. for the absolute value of <code>foo ()</code>.
  90. </p>
  91. <p>The last thing in the compound statement should be an expression
  92. followed by a semicolon; the value of this subexpression serves as the
  93. value of the entire construct. (If you use some other kind of statement
  94. last within the braces, the construct has type <code>void</code>, and thus
  95. effectively no value.)
  96. </p>
  97. <p>This feature is especially useful in making macro definitions &ldquo;safe&rdquo; (so
  98. that they evaluate each operand exactly once). For example, the
  99. &ldquo;maximum&rdquo; function is commonly defined as a macro in standard C as
  100. follows:
  101. </p>
  102. <div class="smallexample">
  103. <pre class="smallexample">#define max(a,b) ((a) &gt; (b) ? (a) : (b))
  104. </pre></div>
  105. <a name="index-side-effects_002c-macro-argument"></a>
  106. <p>But this definition computes either <var>a</var> or <var>b</var> twice, with bad
  107. results if the operand has side effects. In GNU C, if you know the
  108. type of the operands (here taken as <code>int</code>), you can define
  109. the macro safely as follows:
  110. </p>
  111. <div class="smallexample">
  112. <pre class="smallexample">#define maxint(a,b) \
  113. ({int _a = (a), _b = (b); _a &gt; _b ? _a : _b; })
  114. </pre></div>
  115. <p>Embedded statements are not allowed in constant expressions, such as
  116. the value of an enumeration constant, the width of a bit-field, or
  117. the initial value of a static variable.
  118. </p>
  119. <p>If you don&rsquo;t know the type of the operand, you can still do this, but you
  120. must use <code>typeof</code> or <code>__auto_type</code> (see <a href="Typeof.html#Typeof">Typeof</a>).
  121. </p>
  122. <p>In G++, the result value of a statement expression undergoes array and
  123. function pointer decay, and is returned by value to the enclosing
  124. expression. For instance, if <code>A</code> is a class, then
  125. </p>
  126. <div class="smallexample">
  127. <pre class="smallexample"> A a;
  128. ({a;}).Foo ()
  129. </pre></div>
  130. <p>constructs a temporary <code>A</code> object to hold the result of the
  131. statement expression, and that is used to invoke <code>Foo</code>.
  132. Therefore the <code>this</code> pointer observed by <code>Foo</code> is not the
  133. address of <code>a</code>.
  134. </p>
  135. <p>In a statement expression, any temporaries created within a statement
  136. are destroyed at that statement&rsquo;s end. This makes statement
  137. expressions inside macros slightly different from function calls. In
  138. the latter case temporaries introduced during argument evaluation are
  139. destroyed at the end of the statement that includes the function
  140. call. In the statement expression case they are destroyed during
  141. the statement expression. For instance,
  142. </p>
  143. <div class="smallexample">
  144. <pre class="smallexample">#define macro(a) ({__typeof__(a) b = (a); b + 3; })
  145. template&lt;typename T&gt; T function(T a) { T b = a; return b + 3; }
  146. void foo ()
  147. {
  148. macro (X ());
  149. function (X ());
  150. }
  151. </pre></div>
  152. <p>has different places where temporaries are destroyed. For the
  153. <code>macro</code> case, the temporary <code>X</code> is destroyed just after
  154. the initialization of <code>b</code>. In the <code>function</code> case that
  155. temporary is destroyed when the function returns.
  156. </p>
  157. <p>These considerations mean that it is probably a bad idea to use
  158. statement expressions of this form in header files that are designed to
  159. work with C++. (Note that some versions of the GNU C Library contained
  160. header files using statement expressions that lead to precisely this
  161. bug.)
  162. </p>
  163. <p>Jumping into a statement expression with <code>goto</code> or using a
  164. <code>switch</code> statement outside the statement expression with a
  165. <code>case</code> or <code>default</code> label inside the statement expression is
  166. not permitted. Jumping into a statement expression with a computed
  167. <code>goto</code> (see <a href="Labels-as-Values.html#Labels-as-Values">Labels as Values</a>) has undefined behavior.
  168. Jumping out of a statement expression is permitted, but if the
  169. statement expression is part of a larger expression then it is
  170. unspecified which other subexpressions of that expression have been
  171. evaluated except where the language definition requires certain
  172. subexpressions to be evaluated before or after the statement
  173. expression. In any case, as with a function call, the evaluation of a
  174. statement expression is not interleaved with the evaluation of other
  175. parts of the containing expression. For example,
  176. </p>
  177. <div class="smallexample">
  178. <pre class="smallexample"> foo (), (({ bar1 (); goto a; 0; }) + bar2 ()), baz();
  179. </pre></div>
  180. <p>calls <code>foo</code> and <code>bar1</code> and does not call <code>baz</code> but
  181. may or may not call <code>bar2</code>. If <code>bar2</code> is called, it is
  182. called after <code>foo</code> and before <code>bar1</code>.
  183. </p>
  184. <hr>
  185. <div class="header">
  186. <p>
  187. 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> &nbsp; [<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>
  188. </div>
  189. </body>
  190. </html>