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

📄 statement-exprs.html

📁 gcc手册
💻 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.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,

1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.2 or

any later version published by the Free Software Foundation; with the

Invariant Sections being "GNU General Public License" and "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".



   <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.-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Statement%20Exprs">Statement Exprs</a>,

Next:<a rel="next" accesskey="n" href="Local-Labels.html#Local%20Labels">Local Labels</a>,

Up:<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 expression

in GNU C.  This allows you to use loops, switches, and local variables

within an expression.



   <p>Recall that a compound statement is a sequence of statements surrounded

by braces; in this construct, parentheses go around the braces.  For

example:



<pre class="example">     ({ 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) expression

for the absolute value of <code>foo ()</code>.



   <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>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:



<pre class="example">     #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 bad

results if the operand has side effects.  In GNU C, if you know the

type of the operands (here let's assume <code>int</code>), you can define

the macro safely as follows:



<pre class="example">     #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 as

the value of an enumeration constant, the width of a bit-field, or

the initial value of a static variable.



   <p>If you don't know the type of the operand, you can still do this, but you

must use <code>typeof</code> (see <a href="Typeof.html#Typeof">Typeof</a>).



   <p>Statement expressions are not supported fully in G++, and their fate

there is unclear.  (It is possible that they will become fully supported

at some point, or that they will be deprecated, or that the bugs that

are present will continue to exist indefinitely.)  Presently, statement

expressions do not work well as default arguments.



   <p>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:



<pre class="example">     #define foo(a)  ({int b = (a); b + 3; })

     </pre>



<p>does not work the same way as:



<pre class="example">     inline int foo(int a) { int b = a; return b + 3; }

     </pre>



<p>In particular, if the expression passed into <code>foo</code> involves the

creation of temporaries, the destructors for those temporaries will be

run earlier in the case of the macro than in the case of the function.



   <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-expression that lead to precisely this

bug.)



   </body></html>



⌨️ 快捷键说明

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