Title
When exactly is storage reused?
Status
drwp
Section
6.7.4 [basic.life]
Submitter
Richard Smith

Created on 2023-03-23.00:00:00 last changed 12 months ago

Messages

Date: 2023-05-12.20:59:33

Proposed resolution (approved by CWG 2023-05-12):

Change in 6.7.4 [basic.life] paragraph 1 as follows:

The lifetime of an object o of type T ends when:
  • if T is a non-class type, the object is destroyed, or
  • if T is a class type, the destructor call starts, or
  • the storage which the object occupies is released, or is reused by an object that is not nested within o (6.7.2 [intro.object]).
When evaluating a new-expression, storage is considered reused after it is returned from the allocation function, but before the evaluation of the new-initializer (7.6.2.8 [expr.new]). [ Example:
  struct S {
    int m;
  };

  void f() {
    S x{1};
    new(&x) S(x.m);  // undefined behavior
  }
-- end example ]
Date: 2023-06-15.00:00:00

[Accepted as a DR at the June, 2023 meeting.]

Subclause 6.7.4 [basic.life] bullet 1.5 specifies:

The lifetime of an object o of type T ends when:
  • if T is a non-class type, the object is destroyed, or
  • if T is a class type, the destructor call starts, or
  • the storage which the object occupies is released, or is reused by an object that is not nested within o (6.7.2 [intro.object]).

Consider the expression new (p) T(x). Does the lifetime of *p end when p is returned from the allocation function, before x is evaluated? Or does the lifetime end when the constructor body starts executing, after x is evaluated?

The second option is conceivable for initialization by constructor and non-class types; for aggregate initialization, the first option must be used, because evaluation of x directly initializes a part of the resulting object. The first option is simpler to implement for constant evaluation.

History
Date User Action Args
2023-12-19 10:15:28adminsetstatus: dr -> drwp
2023-07-16 13:00:43adminsetstatus: ready -> dr
2023-05-12 20:59:33adminsetmessages: + msg7280
2023-05-12 20:59:33adminsetstatus: open -> ready
2023-03-23 00:00:00admincreate