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 union
keyword or with a typedef
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 Compound Literals.
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:
both x
and y
can be cast to type union foo
and the following assignments
are shorthand equivalents of these
However, (union foo) FLT_MAX;
is not a valid cast because the union has no member of type float
.
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
You can also use the union cast as a function argument:
Next: Mixed Declarations, Previous: Case Ranges, Up: C Extensions [Contents][Index]
© Free Software Foundation
Licensed under the GNU Free Documentation License, Version 1.3.
https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/Cast-to-Union.html