Title
Value initialization of union
Status
cd3
Section
9.4 [dcl.init]
Submitter
Jason Merrill

Created on 2011-04-18.00:00:00 last changed 130 months ago

Messages

Date: 2012-02-15.00:00:00

[Voted into the WP at the February, 2012 meeting; moved to DR at the October, 2012 meeting.]

Date: 2011-08-15.00:00:00

Proposed resolution (August, 2011):

  1. Change 9.4 [dcl.init] paragraph 7 as follows:

  2. To value-initialize an object of type T means:

    • if T is a (possibly cv-qualified) class type (Clause 11 [class]) with either no default constructor (11.4.5 [class.ctor]) or a default constructor that is user-provided or deleted constructor (11.4.5 [class.ctor]), then the object is default-initialized default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

    • if T is a (possibly cv-qualified) non-union class type without a user-provided or deleted default constructor, then the object is zero-initialized and, if T's implicitly-declared default constructor is T has a non-trivial default constructor, that constructor is called. default-initialized;

    • ...

  3. Change 9.4.5 [dcl.init.list] paragraph 3 as follows:

  4. List-initialization of an object or reference of type T is defined as follows:

    • If the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized.

    • Otherwise, if If T is an aggregate, aggregate initialization is performed (9.4.2 [dcl.init.aggr]). [Example:...

    • Otherwise, if the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized.

    • ...

This resolution also resolves issues 1324 and 1368.

Date: 2022-02-18.07:47:23

According to 9.4 [dcl.init] paragraph 7,

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 11 [class]) with a user-provided constructor (11.4.5 [class.ctor]), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

  • if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T's implicitly-declared default constructor is non-trivial, that constructor is called.

  • if T is an array type, then each element is value-initialized;

  • otherwise, the object is zero-initialized.

This suggests that for

  struct A { A() = delete; };
  union B { A a };
  int main()
  {
    B();
  }

a B temporary is created and zero-initialized, even though its default constructor is deleted. We should strike "non-union" and also the "if...non-trivial" condition, since we can have a trivial deleted constructor.

History
Date User Action Args
2014-03-03 00:00:00adminsetstatus: drwp -> cd3
2012-11-03 00:00:00adminsetstatus: dr -> drwp
2012-09-24 00:00:00adminsetmessages: + msg4006
2012-02-27 00:00:00adminsetmessages: + msg3815
2012-02-27 00:00:00adminsetstatus: ready -> dr
2011-04-18 00:00:00admincreate