summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/typeof.html
blob: c487e7a124775fb6279930ad09d657f702bc0e22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<div class="section-level-extent" id="Typeof"> <div class="nav-panel"> <p> Next: <a href="conditionals" accesskey="n" rel="next">Conditionals with Omitted Operands</a>, Previous: <a href="constructing-calls" accesskey="p" rel="prev">Constructing Function Calls</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div>  <h1 class="section" id="Referring-to-a-Type-with-typeof"><span>6.7 Referring to a Type with typeof<a class="copiable-link" href="#Referring-to-a-Type-with-typeof"> ¶</a></span></h1>    <p>Another way to refer to the type of an expression is with <code class="code">typeof</code>. The syntax of using of this keyword looks like <code class="code">sizeof</code>, but the construct acts semantically like a type name defined with <code class="code">typedef</code>. </p> <p>There are two ways of writing the argument to <code class="code">typeof</code>: with an expression or with a type. Here is an example with an expression: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">typeof (x[0](1))</pre>
</div> <p>This assumes that <code class="code">x</code> is an array of pointers to functions; the type described is that of the values of the functions. </p> <p>Here is an example with a typename as the argument: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">typeof (int *)</pre>
</div> <p>Here the type described is that of pointers to <code class="code">int</code>. </p> <p>If you are writing a header file that must work when included in ISO C programs, write <code class="code">__typeof__</code> instead of <code class="code">typeof</code>. See <a class="xref" href="alternate-keywords">Alternate Keywords</a>. </p> <p>A <code class="code">typeof</code> construct can be used anywhere a typedef name can be used. For example, you can use it in a declaration, in a cast, or inside of <code class="code">sizeof</code> or <code class="code">typeof</code>. </p> <p>The operand of <code class="code">typeof</code> is evaluated for its side effects if and only if it is an expression of variably modified type or the name of such a type. </p> <p><code class="code">typeof</code> is often useful in conjunction with statement expressions (see <a class="pxref" href="statement-exprs">Statements and Declarations in Expressions</a>). Here is how the two together can be used to define a safe “maximum” macro which operates on any arithmetic type and evaluates each of its arguments exactly once: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define max(a,b) \
  ({ typeof (a) _a = (a); \
      typeof (b) _b = (b); \
    _a &gt; _b ? _a : _b; })</pre>
</div>      <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 class="code">a</code> and <code class="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> <p>Some more examples of the use of <code class="code">typeof</code>: </p> <ul class="itemize mark-bullet"> <li>This declares <code class="code">y</code> with the type of what <code class="code">x</code> points to. <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">typeof (*x) y;</pre>
</div> </li>
<li>This declares <code class="code">y</code> as an array of such values. <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">typeof (*x) y[4];</pre>
</div> </li>
<li>This declares <code class="code">y</code> as an array of pointers to characters: <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">typeof (typeof (char *)[4]) y;</pre>
</div> <p>It is equivalent to the following traditional C declaration: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">char *y[4];</pre>
</div> <p>To see the meaning of the declaration using <code class="code">typeof</code>, and why it might be a useful way to write, rewrite it with these macros: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define pointer(T)  typeof(T *)
#define array(T, N) typeof(T [N])</pre>
</div> <p>Now the declaration can be rewritten this way: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">array (pointer (char), 4) y;</pre>
</div> <p>Thus, <code class="code">array (pointer (char), 4)</code> is the type of arrays of 4 pointers to <code class="code">char</code>. </p>
</li>
</ul> <p>In GNU C, but not GNU C++, you may also declare the type of a variable as <code class="code">__auto_type</code>. In that case, the declaration must declare only one variable, whose declarator must just be an identifier, the declaration must be initialized, and the type of the variable is determined by the initializer; the name of the variable is not in scope until after the initializer. (In C++, you should use C++11 <code class="code">auto</code> for this purpose.) Using <code class="code">__auto_type</code>, the “maximum” macro above could be written as: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define max(a,b) \
  ({ __auto_type _a = (a); \
      __auto_type _b = (b); \
    _a &gt; _b ? _a : _b; })</pre>
</div> <p>Using <code class="code">__auto_type</code> instead of <code class="code">typeof</code> has two advantages: </p> <ul class="itemize mark-bullet"> <li>Each argument to the macro appears only once in the expansion of the macro. This prevents the size of the macro expansion growing exponentially when calls to such macros are nested inside arguments of such macros. </li>
<li>If the argument to the macro has variably modified type, it is evaluated only once when using <code class="code">__auto_type</code>, but twice if <code class="code">typeof</code> is used. </li>
</ul> </div>  <div class="nav-panel"> <p> Next: <a href="conditionals">Conditionals with Omitted Operands</a>, Previous: <a href="constructing-calls">Constructing Function Calls</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
  <p class="_attribution-p">
    &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
    <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Typeof.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Typeof.html</a>
  </p>
</div>