Movatterモバイル変換


[0]ホーム

URL:


Issue 1301 - WG21 CWG Issues
Title
Value initialization of union
Status
cd3
Section
9.5 [dcl.init]
Submitter
Jason Merrill

Created on2011-04-18.00:00:00 last changed145 months ago

Messages

msg4006 (view)
Date: 2012-02-15.00:00:00

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

msg3815 (view)
Date: 2011-08-15.00:00:00

Proposed resolution (August, 2011):

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

  2. Tovalue-initialize an object of typeT means:

    • ifT is a (possibly cv-qualified) class type(Clause 11 [class]) witheither no defaultconstructor (11.4.5 [class.ctor]) or adefaultconstructor that is user-providedor deletedconstructor (11.4.5 [class.ctor]), then theobject is default-initializeddefault constructor forT is called (and the initialization is ill-formed ifT has no accessible default constructor);

    • ifT is a (possibly cv-qualified) non-union class typewithout a user-providedor deleted default constructor,then the object is zero-initialized and, ifT'simplicitly-declared default constructor isT hasa non-trivialdefault constructor,thatconstructor is called.default-initialized;

    • ...

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

  4. List-initialization of an object or reference of typeT isdefined as follows:

    • If the initializer list has no elements andTis a class type with a default constructor, the object isvalue-initialized.

    • Otherwise, ifIfT is anaggregate, aggregate initialization is performed (9.5.2 [dcl.init.aggr]). [Example:...

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

    • ...

This resolution also resolves issues1324and1368.

msg3467 (view)
Date: 2022-02-18.07:47:23

According to 9.5 [dcl.init] paragraph 7,

Tovalue-initialize an object of typeT means:

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

  • ifT is a (possibly cv-qualified) non-union class typewithout a user-provided constructor, then the object iszero-initialized and, ifT's implicitly-declared defaultconstructor is non-trivial, that constructor is called.

  • ifT is an array type, then each element isvalue-initialized;

  • otherwise, the object is zero-initialized.

This suggests that for

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

aB temporary is created and zero-initialized, even thoughits default constructor is deleted. We should strike "non-union" andalso the "if...non-trivial" condition, since we can have a trivialdeleted constructor.

History
DateUserActionArgs
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

[8]ページ先頭

©2009-2026 Movatter.jp