The transparent_union type attribute (C only)

The transparent_union attribute applied to a union definition or a union typedef definition indicates the union can be used as a transparent union. Whenever a transparent union is the type of a function parameter and that function is called, the transparent union can accept an argument of any type that matches that of one of its members without an explicit cast. Arguments to this function parameter are passed to the transparent union, using the calling convention of the first member of the union type. Because of this, all members of the union must have the same machine representation. Transparent unions are useful in library functions that use multiple interfaces to resolve issues of compatibility.

Read syntax diagramSkip visual syntax diagramtransparent_union type attribute syntax
 
>>-__attribute__--((--+-transparent_union-----+--))------------><
                      '-__transparent_union__-'
 

The union must be a complete union type. The transparent_union type attribute can be applied to anonymous unions with tag names.

When the transparent_union type attribute is applied to the outer union of a nested union, the size of the inner union (that is, its largest member) is used to determine if it has the same machine representation as the other members of the outer union. For example,

union __attribute__((__transparent_union__)) u_t {
   	  union u2_t {
      char a;
      short b;
      char c;
      char d;
   };
   int a;
} ;

the attribute is ignored because the first member of union u_t, which is itself a union, has a machine representation of 2 bytes, whereas the other member of union u_t is of type int, which has a machine representation of 4 bytes.

The same rationale applies to members of a union that are structures. When a member of a union to which type attribute transparent_union has been applied is a struct, the machine representation of the entire struct is considered, rather than members.

All members of the union must have the same machine representation as the first member of the union. This means that all members must be representable by the same amount memory as the first member of the union. The machine representation of the first member represents the maximum memory size for any remaining union members. For instance, if the first member of a union to which type attribute transparent_union has been applied is of type int, then all following members must be representable by at most 4 bytes. Members that are representable by 1, 2, or 4 bytes are considered valid for this transparent union.

Floating-point types (float, double, float _Complex, or double _Complex) types or vector types can be members of a transparent union, but they cannot be the first member. The restriction that all members of the transparent union have the same machine representation as the first member still applies.

End of IBM extension