This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-11-11


1702. Rephrasing the definition of “anonymous union”

Section: 11.5  [class.union]     Status: drafting     Submitter: Richard Smith     Date: 2013-06-17

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

A union of the form

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; };

Additional notes (July, 2023)

This issue is addressed by issue 2767.