Title
Creating objects within a mutable member of a const object
Status
open
Section
6.7.4 [basic.life]
Submitter
Jiang An

Created on 2023-07-17.00:00:00 last changed 22 months ago

Messages

Date: 2023-07-17.00:00:00

(From submission #367.)

Consider:

  struct S {
    alignas(int) mutable unsigned char buf[sizeof(int)];
  };
  int f() {
    const S s{};
    ::new ((void*)s.buf) int{42};
  }

Creating a new object in the mutable portion of a const complete object is undefined behavior per 6.7.4 [basic.life] paragraph 12. However, this ought to be allowed.

Possible resolution:

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

Creating a new object O within the storage that a const, complete object X with static, thread, or automatic storage duration occupies, or within the storage that such a const object used to occupy before its lifetime ended, results in undefined behavior unless O is nested within a mutable subobject of X. [ Example: ... ]
History
Date User Action Args
2023-07-17 00:00:00admincreate