blob: db9be2ba7b9d7c943e76604637782fd95eb55168 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<div class="section-level-extent" id="Cast-to-Union"> <div class="nav-panel"> <p> Next: <a href="mixed-labels-and-declarations" accesskey="n" rel="next">Mixed Declarations, Labels and Code</a>, Previous: <a href="case-ranges" accesskey="p" rel="prev">Case Ranges</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="Cast-to-a-Union-Type"><span>6.31 Cast to a Union Type<a class="copiable-link" href="#Cast-to-a-Union-Type"> ¶</a></span></h1> <p>A cast to a union type is a C extension not available in C++. It looks just like ordinary casts with the constraint that the type specified is a union type. You can specify the type either with the <code class="code">union</code> keyword or with a <code class="code">typedef</code> name that refers to a union. The result of a cast to a union is a temporary rvalue of the union type with a member whose type matches that of the operand initialized to the value of the operand. The effect of a cast to a union is similar to a compound literal except that it yields an rvalue like standard casts do. See <a class="xref" href="compound-literals">Compound Literals</a>. </p> <p>Expressions that may be cast to the union type are those whose type matches at least one of the members of the union. Thus, given the following union and variables: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">union foo { int i; double d; };
int x;
double y;
union foo z;</pre>
</div> <p>both <code class="code">x</code> and <code class="code">y</code> can be cast to type <code class="code">union foo</code> and the following assignments </p>
<div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">z = (union foo) x;
z = (union foo) y;</pre>
</div> <p>are shorthand equivalents of these </p>
<div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">z = (union foo) { .i = x };
z = (union foo) { .d = y };</pre>
</div> <p>However, <code class="code">(union foo) FLT_MAX;</code> is not a valid cast because the union has no member of type <code class="code">float</code>. </p> <p>Using the cast as the right-hand side of an assignment to a variable of union type is equivalent to storing in a member of the union with the same type </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">union foo u;
/* <span class="r">…</span> */
u = (union foo) x ≡ u.i = x
u = (union foo) y ≡ u.d = y</pre>
</div> <p>You can also use the union cast as a function argument: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void hack (union foo);
/* <span class="r">…</span> */
hack ((union foo) x);</pre>
</div> </div> <div class="nav-panel"> <p> Next: <a href="mixed-labels-and-declarations">Mixed Declarations, Labels and Code</a>, Previous: <a href="case-ranges">Case Ranges</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">
© 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/Cast-to-Union.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Cast-to-Union.html</a>
</p>
</div>
|