[class.union.anon]

# 11 Classes [[class]](./#class)

## 11.5 Unions [[class.union]](class.union#anon)

### 11.5.2 Anonymous unions [class.union.anon]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3349)

A union of the form

union { [*member-specification*](class.mem.general#nt:member-specification "11.4.1 General [class.mem.general]") } ;

is called an [*anonymous union*](#def:anonymous_union "11.5.2 Anonymous unions [class.union.anon]"); it defines an unnamed type and
an unnamed object of that type called
an [*anonymous union member*](#def:member,anonymous_union "11.5.2 Anonymous unions [class.union.anon]") if it is a non-static data member or
an [*anonymous union variable*](#def:variable,anonymous_union "11.5.2 Anonymous unions [class.union.anon]") otherwise[.](#1.sentence-1)

Each [*member-declaration*](class.mem.general#nt:member-declaration "11.4.1 General [class.mem.general]") in the [*member-specification*](class.mem.general#nt:member-specification "11.4.1 General [class.mem.general]") of an anonymous union shall either define one or more public non-static data members or
be a [*static_assert-declaration*](dcl.pre#nt:static_assert-declaration "9.1 Preamble [dcl.pre]")[.](#1.sentence-2)

Nested types, anonymous unions, and functions
shall not be declared within an anonymous union[.](#1.sentence-3)

The names of the members of an anonymous union
are bound in the scope inhabited by the union declaration[.](#1.sentence-4)

[*Example [1](#example-1)*: void f() {union { int a; const char* p; };
 a = 1;
 p = "Jennifer";}

Here a and p are used like ordinary (non-member)
variables, but since they are union members they have the same address[.](#1.sentence-5)

â *end example*]

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3383)

An anonymous union declared in the scope of a namespace with external linkage
shall use the [*storage-class-specifier*](dcl.stc#nt:storage-class-specifier "9.2.2 Storage class specifiers [dcl.stc]") static[.](#2.sentence-1)

Anonymous unions declared at block scope shall not use a [*storage-class-specifier*](dcl.stc#nt:storage-class-specifier "9.2.2 Storage class specifiers [dcl.stc]") that is not permitted in the declaration of a block variable[.](#2.sentence-2)

An anonymous union declaration at class scope shall not have
a [*storage-class-specifier*](dcl.stc#nt:storage-class-specifier "9.2.2 Storage class specifiers [dcl.stc]")[.](#2.sentence-3)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3393)

[*Note [1](#note-1)*:

A union for which objects, pointers, or references are declared is not an anonymous union[.](#3.sentence-1)

[*Example [2](#example-2)*: void f() {union { int aa; char* p; } obj, *ptr = &obj;
 aa = 1; // error ptr->aa = 1; // OK}

The assignment to plain aa is ill-formed since the member name
is not visible outside the union, and even if it were visible, it is not
associated with any particular object[.](#3.sentence-2)

â *end example*]

â *end note*]

[*Note [2](#note-2)*:

Initialization of unions with no user-declared constructors is described
in [[dcl.init.aggr]](dcl.init.aggr "9.5.2 Aggregates")[.](#3.sentence-3)

â *end note*]

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3415)

A [*union-like class*](#def:class,union-like "11.5.2 Anonymous unions [class.union.anon]") is a union or a class that has an anonymous union as a direct
member[.](#4.sentence-1)

A union-like class X has a set of [*variant members*](#def:variant_member "11.5.2 Anonymous unions [class.union.anon]")[.](#4.sentence-2)

If X is a union, a non-static data member of X that is not an anonymous
union is a variant member of X[.](#4.sentence-3)

In addition, a non-static data member of an
anonymous union that is a member of X is also a variant member of X[.](#4.sentence-4)

At most one variant member of a union may have a default member initializer[.](#4.sentence-5)

[*Example [3](#example-3)*: union U {int x = 0; union {int k; }; union {int z; int y = 1; // error: initialization for second variant member of U};}; â *end example*]
