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

📄 typeof.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="Typeof">Typeof</a>,

Next:<a rel="next" accesskey="n" href="Lvalues.html#Lvalues">Lvalues</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 the

construct 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 an

expression or with a type.  Here is an example with an expression:



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

programs, 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 be

used.  For example, you can use it in a declaration, in a cast, or inside

of <code>sizeof</code> or <code>typeof</code>.



   <p><code>typeof</code> is often useful in conjunction with the

statements-within-expressions feature.  Here is how the two together can

be used to define a safe "maximum" macro that operates on any

arithmetic type and evaluates each of its arguments exactly once:



<pre class="example">     #define max(a,b) \

       ({ typeof (a) _a = (a); \

           typeof (b) _b = (b); \

         _a &gt; _b ? _a : _b; })

     </pre>



   <p>The reason for using names that start with underscores for the local

variables is to avoid conflicts with variable names that occur within the

expressions that are substituted for <code>a</code> and <code>b</code>.  Eventually we

hope to design a new form of declaration syntax that allows you to declare

variables whose scopes start only after their initializers; this will be a

more 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="example">          typeof (*x) y;

          </pre>



     <li>This declares <code>y</code> as an array of such values.



     <pre class="example">          typeof (*x) y[4];

          </pre>



     <li>This declares <code>y</code> as an array of pointers to characters:



     <pre class="example">          typeof (typeof (char *)[4]) y;

          </pre>



     <p>It is equivalent to the following traditional C declaration:



     <pre class="example">          char *y[4];

          </pre>



     <p>To see the meaning of the declaration using <code>typeof</code>, and why it

might be a useful way to write, let's rewrite it with these macros:



     <pre class="example">          #define pointer(T)  typeof(T *)

          #define array(T, N) typeof(T [N])

          </pre>



     <p>Now the declaration can be rewritten this way:



     <pre class="example">          array (pointer (char), 4) y;

          </pre>



     <p>Thus, <code>array (pointer (char), 4)</code> is the type of arrays of 4

pointers to <code>char</code>. 

</ul>



   <p><em>Compatibility Note:</em> In addition to <code>typeof</code>, GCC 2 supported

a more limited extension which permitted one to write



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

3.0 and 3.2 will crash; 3.2.1 and later give an error).  Code which

relies on it should be rewritten to use <code>typeof</code>:



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