Title
Non-static data member initializers and union ctor-initializer
Status
c++14
Section
11.9.3 [class.base.init]
Submitter
Richard Smith

Created on 2012-10-01.00:00:00 last changed 121 months ago

Messages

Date: 2013-09-15.00:00:00

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

Date: 2013-08-15.00:00:00

Proposed resolution (August, 2013):

This issue is resolved by the resolution of issue 1460.

Date: 2013-03-15.00:00:00

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

  • if the entity is a non-static data member that has a brace-or-equal-initializer and, if the entity is a member of a union, no other non-static data member of that union is designated by a mem-initializer-id, the entity is initialized as specified in 9.4 [dcl.init];

  • ...

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

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

Date: 2013-05-03.00:00:00

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.)

History
Date User Action Args
2014-11-24 00:00:00adminsetstatus: drwp -> c++14
2014-03-03 00:00:00adminsetstatus: dr -> drwp
2013-10-14 00:00:00adminsetmessages: + msg4693
2013-10-14 00:00:00adminsetstatus: tentatively ready -> dr
2013-09-03 00:00:00adminsetmessages: + msg4463
2013-09-03 00:00:00adminsetstatus: drafting -> tentatively ready
2013-05-03 00:00:00adminsetmessages: + msg4342
2012-10-01 00:00:00admincreate