This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 116a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-01-20


2986. Creating objects within a mutable member of a const object

Section: 6.7.4  [basic.life]     Status: open     Submitter: Jiang An     Date: 2023-07-17

(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: ... ]