Title
Lifetime, zero-initialization, and dynamic initialization
Status
review
Section
6.7.4 [basic.life]
Submitter
Jan Schultke

Created on 2023-07-24.00:00:00 last changed 10 months ago

Messages

Date: 2024-02-17.11:33:06

CWG 2024-01-19

The first option is is intended.

Possible resolution:

Split and change 6.7.4 [basic.life] paragraph 1 as follows:

... The lifetime of an object of type T begins when:

  • storage with the proper alignment and size for type T is obtained, and
  • its any initialization (if any) is complete (including vacuous initialization) (9.4 [dcl.init]),
except that if the object is a union member or subobject thereof, its lifetime only begins if that union member is the initialized member in the union (9.4.2 [dcl.init.aggr], 11.9.3 [class.base.init]), or as described in 11.5 [class.union], 11.4.5.3 [class.copy.ctor], and 11.4.6 [class.copy.assign], and except as described in 20.2.10.2 [allocator.members]. [ Note: In particular, the lifetime of a complete class object begins when the non-delegating constructor returns (11.9.3 [class.base.init]). The lifetime of a complete object with static or thread storage duration begins when static initialization is complete (6.9.3.2 [basic.start.static]). -- end note ]

The lifetime of an object o of type T ends when: ...

Date: 2023-07-24.00:00:00

Consider:

  int i = i;

According to 6.9.3.2 [basic.start.static] paragraph 2, i is zero-initialized and then dynamic initialization is applied. However, it is unclear whether the lifetime of i has started at the point when the dynamic initialization occurs. According to 6.7.4 [basic.life] paragraph 1:

... The lifetime of an object of type T begins when:
  • storage with the proper alignment and size for type T is obtained, and
  • its initialization (if any) is complete (including vacuous initialization) (9.4 [dcl.init]),
except ...

Possible directions:

  • Zero-initialization starts the lifetime, and the dynamic initialization just modifies the value, similar to a delegating constructor.
  • The lifetime doesn't begin until the dynamic initialization is complete, but we allow scalar objects to be "under construction" (11.9.5 [class.cdtor]).
  • Zero-initialization starts the lifetime, and dynamic initialization transparently replaces the object.

The example in 6.9.3.2 [basic.start.static] paragraph 3 suggests the first option.

History
Date User Action Args
2024-02-17 11:33:06adminsetmessages: + msg7611
2024-02-17 11:33:06adminsetstatus: open -> review
2023-07-24 00:00:00admincreate