⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statement-exprs.html

📁 自己收集的linux入门到学懂高级编程书集 包括linux程序设计第三版
💻 HTML
字号:
<html lang="en"><head><title>Using the GNU Compiler Collection (GCC)</title><meta http-equiv="Content-Type" content="text/html"><meta name="description" content="Using the GNU Compiler Collection (GCC)"><meta name="generator" content="makeinfo 4.6"><!--Copyright &copy; 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.   <p>Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being "GNU General Public License" and "FundingFree Software", the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below).  A copy of the license isincluded in the section entitled "GNU Free Documentation License".   <p>(a) The FSF's Front-Cover Text is:   <p>A GNU Manual   <p>(b) The FSF's Back-Cover Text is:   <p>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.--><meta http-equiv="Content-Style-Type" content="text/css"><style type="text/css"><!--  pre.display { font-family:inherit }  pre.format  { font-family:inherit }  pre.smalldisplay { font-family:inherit; font-size:smaller }  pre.smallformat  { font-family:inherit; font-size:smaller }  pre.smallexample { font-size:smaller }  pre.smalllisp    { font-size:smaller }--></style></head><body><div class="node"><p>Node:&nbsp;<a name="Statement%20Exprs">Statement Exprs</a>,Next:&nbsp;<a rel="next" accesskey="n" href="Local-Labels.html#Local%20Labels">Local Labels</a>,Up:&nbsp;<a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a><hr><br></div><h3 class="section">Statements and Declarations in Expressions</h3><p>A compound statement enclosed in parentheses may appear as an expressionin GNU C.  This allows you to use loops, switches, and local variableswithin an expression.   <p>Recall that a compound statement is a sequence of statements surroundedby braces; in this construct, parentheses go around the braces.  Forexample:<pre class="smallexample">     ({ int y = foo (); int z;        if (y &gt; 0) z = y;        else z = - y;        z; })     </pre><p>is a valid (though slightly more complex than necessary) expressionfor the absolute value of <code>foo ()</code>.   <p>The last thing in the compound statement should be an expressionfollowed by a semicolon; the value of this subexpression serves as thevalue of the entire construct.  (If you use some other kind of statementlast within the braces, the construct has type <code>void</code>, and thuseffectively no value.)   <p>This feature is especially useful in making macro definitions "safe" (sothat they evaluate each operand exactly once).  For example, the"maximum" function is commonly defined as a macro in standard C asfollows:<pre class="smallexample">     #define max(a,b) ((a) &gt; (b) ? (a) : (b))     </pre><p>But this definition computes either <var>a</var> or <var>b</var> twice, with badresults if the operand has side effects.  In GNU C, if you know thetype of the operands (here let's assume <code>int</code>), you can definethe macro safely as follows:<pre class="smallexample">     #define maxint(a,b) \       ({int _a = (a), _b = (b); _a &gt; _b ? _a : _b; })     </pre>   <p>Embedded statements are not allowed in constant expressions, such asthe value of an enumeration constant, the width of a bit-field, orthe initial value of a static variable.   <p>If you don't know the type of the operand, you can still do this, but youmust use <code>typeof</code> (see <a href="Typeof.html#Typeof">Typeof</a>).   <p>In G++, the result value of a statement expression undergoes array andfunction pointer decay, and is returned by value to the enclosingexpression. For instance, if <code>A</code> is a class, then<pre class="smallexample">             A a;                  ({a;}).Foo ()     </pre><p>will construct a temporary <code>A</code> object to hold the result of thestatement expression, and that will be used to invoke <code>Foo</code>. Therefore the <code>this</code> pointer observed by <code>Foo</code> will not be theaddress of <code>a</code>.   <p>Any temporaries created within a statement within a statement expressionwill be destroyed at the statement's end.  This makes statementexpressions inside macros slightly different from function calls.  Inthe latter case temporaries introduced during argument evaluation willbe destroyed at the end of the statement that includes the functioncall.  In the statement expression case they will be destroyed duringthe statement expression.  For instance,<pre class="smallexample">     #define macro(a)  ({__typeof__(a) b = (a); b + 3; })     template&lt;typename T&gt; T function(T a) { T b = a; return b + 3; }          void foo ()     {       macro (X ());       function (X ());     }     </pre><p>will have different places where temporaries are destroyed.  For the<code>macro</code> case, the temporary <code>X</code> will be destroyed just afterthe initialization of <code>b</code>.  In the <code>function</code> case thattemporary will be destroyed when the function returns.   <p>These considerations mean that it is probably a bad idea to usestatement-expressions of this form in header files that are designed towork with C++.  (Note that some versions of the GNU C Library containedheader files using statement-expression that lead to precisely thisbug.)   </body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -