Non-defining declarations of anonymous unions
11.5.2 [class.union.anon]
Hubert Tong

Created on 2023-07-06.00:00:00 last changed 4 months ago


Date: 2023-10-21.06:42:09

Currently, the specification permits this example:

  namespace {
    extern union { int x, y; };
    union { int x, y; };

However, those non-defining declarations seem to be mostly useless, because any definition attempt would violate the one-definition rule (6.3 [basic.def.odr]).

Possible resolution (updated 2023-10-21):

This resolution also addresses issue 1702.

  1. Split 11.5.2 [class.union.anon] paragraph 1 and change as follows:

    A union of the form A simple-declaration or member-declaration of the form

       attribute-specifier-seqopt decl-specifier-seq ;
    where one of the decl-specifiers is a class-specifier of the form
      union attribute-specifier-seqopt { member-specification } ;
    is called declares an anonymous union; it defines an unnamed type T and an unnamed object of that type cv T called an anonymous union member if it is a non-static data member or an anonymous union variable otherwise, where cv is determined from the cv-qualifiers of the declaration. The optional attribute-specifier-seq in the simple-declaration or member-declaration appertains to the data member or variable, respectively. The storage-class-specifier extern (9.2.2 [dcl.stc]) shall not be among the decl-specifiers declaring an anonymous union. The declaration of an anonymous union shall not be the name-declaration of a linkage-specification (9.11 [dcl.link]).

    Each member-declaration in the member-specification of an anonymous union shall either define one or more public non-static data members or be a static_assert-declaration. Nested types, anonymous unions, and functions shall not be declared within an anonymous union. The names of the members of an anonymous union are bound in the scope inhabited by the union declaration.

  2. Change in 11.5.2 [class.union.anon] paragraph 2 as follows:

    Anonymous unions declared in the scope of An anonymous union whose declaration inhabits a namespace scope with external linkage shall be declared static. Anonymous unions declared at block scope shall be declared with any storage class allowed for a block variable, or with no storage class. A storage class is not allowed in a declaration of an anonymous union in a class scope. An anonymous union whose declaration inhabits a class scope shall not be declared with a storage-class-specifier.
Date User Action Args
2023-07-06 00:00:00admincreate