Rephrasing the definition of “anonymous union”
11.5 [class.union]
Richard Smith

Created on 2013-06-17.00:00:00 last changed 12 months ago


Date: 2023-07-15.00:00:00

Additional notes (July, 2023)

This issue is addressed by issue 2767.

Date: 2013-06-17.00:00:00

11.5 [class.union] paragraph 5 defines an anonymous union as follows:

A union of the form

    union { member-specification } ;

is called an anonymous union; it defines an unnamed object of unnamed type.

It is obviously intended that a declaration like

    static union { int i; float f; };

is a declaration of that form (cf paragraph 6, which requires the static keyword for anonymous unions declared in namespace scope). However, it would be clearer if the definition were recast in more descriptive terms, e.g.,

An anonymous union is an unnamed class that is defined with the class-key union in a simple-declaration in which the init-declarator-list is omitted. Such a simple-declaration is treated as if it contained a single declarator declaring an unnamed variable of the union's type.

(Note that this definition would require some additional tweaking to apply to class member anonymous union declarations, since simple-declarations are not included as member-declarations.)

As a related point, it is not clear how the following examples are to be treated, and there is implementation variance on some:

   void f() { thread_local union { int a; }; }
   void g() { extern union { int b; }; }
   thread_local union { int c; }; // static is implied by thread_local
   static thread_local union { int d; };
   static const union { int e = 0; }; // is e const? Clang says yes, gcc says no
   static constexpr union { int f = 0; };
Date User Action Args
2023-07-15 18:01:36adminsetmessages: + msg7375
2013-10-14 00:00:00adminsetstatus: open -> drafting
2013-06-17 00:00:00admincreate