📄 typeof.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 © 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: <a name="Typeof">Typeof</a>,Next: <a rel="next" accesskey="n" href="Conditionals.html#Conditionals">Conditionals</a>,Previous: <a rel="previous" accesskey="p" href="Constructing-Calls.html#Constructing%20Calls">Constructing Calls</a>,Up: <a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a><hr><br></div><h3 class="section">Referring to a Type with <code>typeof</code></h3><p>Another way to refer to the type of an expression is with <code>typeof</code>. The syntax of using of this keyword looks like <code>sizeof</code>, but theconstruct acts semantically like a type name defined with <code>typedef</code>. <p>There are two ways of writing the argument to <code>typeof</code>: with anexpression or with a type. Here is an example with an expression:<pre class="smallexample"> typeof (x[0](1)) </pre><p>This assumes that <code>x</code> is an array of pointers to functions;the type described is that of the values of the functions. <p>Here is an example with a typename as the argument:<pre class="smallexample"> typeof (int *) </pre><p>Here the type described is that of pointers to <code>int</code>. <p>If you are writing a header file that must work when included in ISO Cprograms, write <code>__typeof__</code> instead of <code>typeof</code>. See <a href="Alternate-Keywords.html#Alternate%20Keywords">Alternate Keywords</a>. <p>A <code>typeof</code>-construct can be used anywhere a typedef name could beused. For example, you can use it in a declaration, in a cast, or insideof <code>sizeof</code> or <code>typeof</code>. <p><code>typeof</code> is often useful in conjunction with thestatements-within-expressions feature. Here is how the two together canbe used to define a safe "maximum" macro that operates on anyarithmetic type and evaluates each of its arguments exactly once:<pre class="smallexample"> #define max(a,b) \ ({ typeof (a) _a = (a); \ typeof (b) _b = (b); \ _a > _b ? _a : _b; }) </pre> <p>The reason for using names that start with underscores for the localvariables is to avoid conflicts with variable names that occur within theexpressions that are substituted for <code>a</code> and <code>b</code>. Eventually wehope to design a new form of declaration syntax that allows you to declarevariables whose scopes start only after their initializers; this will be amore reliable way to prevent such conflicts.<p>Some more examples of the use of <code>typeof</code>: <ul><li>This declares <code>y</code> with the type of what <code>x</code> points to. <pre class="smallexample"> typeof (*x) y; </pre> <li>This declares <code>y</code> as an array of such values. <pre class="smallexample"> typeof (*x) y[4]; </pre> <li>This declares <code>y</code> as an array of pointers to characters: <pre class="smallexample"> typeof (typeof (char *)[4]) y; </pre> <p>It is equivalent to the following traditional C declaration: <pre class="smallexample"> char *y[4]; </pre> <p>To see the meaning of the declaration using <code>typeof</code>, and why itmight be a useful way to write, let's rewrite it with these macros: <pre class="smallexample"> #define pointer(T) typeof(T *) #define array(T, N) typeof(T [N]) </pre> <p>Now the declaration can be rewritten this way: <pre class="smallexample"> array (pointer (char), 4) y; </pre> <p>Thus, <code>array (pointer (char), 4)</code> is the type of arrays of 4pointers to <code>char</code>. </ul> <p><em>Compatibility Note:</em> In addition to <code>typeof</code>, GCC 2 supporteda more limited extension which permitted one to write<pre class="smallexample"> typedef <var>T</var> = <var>expr</var>; </pre><p>with the effect of declaring <var>T</var> to have the type of the expression<var>expr</var>. This extension does not work with GCC 3 (versions between3.0 and 3.2 will crash; 3.2.1 and later give an error). Code whichrelies on it should be rewritten to use <code>typeof</code>:<pre class="smallexample"> typedef typeof(<var>expr</var>) <var>T</var>; </pre><p>This will work with all versions of GCC. </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -