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

2023-02-07


1587. constexpr initialization and nested anonymous unions

Section: 9.2.6  [dcl.constexpr]     Status: C++14     Submitter: Daveed Vandvoorde     Date: 2012-11-16

[Moved to DR at the September, 2013 meeting.]

After the resolution of issue 1359, one of the requirements for constexpr constructors is:

if the class is a non-empty union, or for each non-empty anonymous union member of a non-union class, exactly one non-static data member shall be initialized;

This wording does not appear to handle nested anonymous unions. For example:

  struct S {
    union {
      union {
        int x = 1;
        float f;
      };
      void *p = nullptr;
    };
  };

Clearly here both S::x and S::p are initialized, but that does not appear to violate the new constraint.

Additional note (March, 2013):

It is not clear whether this example violates 11.5 [class.union] paragraph 5:

The member-specification of an anonymous union shall only define non-static data members. [Note: Nested types and functions cannot be declared within an anonymous union. —end note]

Is a nested anonymous union a “non-static data member” or a “nested type?”

See also issues 57, 1460, 1562, 1587, 1621, and 1623.

Proposed resolution (June, 2013):

Change 11.5 [class.union] paragraph 5 as follows:

...The member-specification of an anonymous union shall only define non-static data members. [Note: Nested types, anonymous unions, and functions cannot be declared within an anonymous union. —end note] The names of the members...