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

2024-10-26


1562. Non-static data member initializers and union ctor-initializer

Section: 11.9.3  [class.base.init]     Status: C++14     Submitter: Richard Smith     Date: 2012-10-01

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

The following example is ill-formed:

  union U {
    int a = 0;
    float f;
    U() {}               // ok, a initialized to 0
    U(float f) : f(f) {} // error, constructor initializes both a and f
  };

because of 11.9.3 [class.base.init] paragraph 8:

An attempt to initialize more than one non-static data member of a union renders the program ill-formed.

In non-union classes, a mem-initializer takes precedence over a non-static data member initializer, per paragraph 9:

If a given non-static data member has both a brace-or-equal-initializer and a mem-initializer, the initialization specified by the mem-initializer is performed, and the non-static data member's brace-or-equal-initializer is ignored.

It would be reasonable to treat union mem-initializers in an analogous fashion.

(See also issue 1460.)

Proposed resolution (March, 2013) [superseded]:

Change 11.9.3 [class.base.init] paragraph 8 as follows:

In a non-delegating constructor, if a given non-static data member or base class is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (11.7.4 [class.abstract]), then

[Note: this wording was reviewed during the 2013-03-25 teleconference.]

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

Proposed resolution (August, 2013):

This issue is resolved by the resolution of issue 1460.