Title
Multiple objects of the same type at the same address
Status
open
Section
6.7.2 [intro.object]
Submitter
Chris Hallock

Created on 2023-06-08.00:00:00 last changed 10 months ago

Messages

Date: 2023-06-09.07:14:31

Suggested resolution:

Change in 6.7.2 [intro.object] paragraph 3 as follows:

If a complete object of type T is created (7.6.2.8 [expr.new]) in storage associated with another object e of type “array of N unsigned char” or of type “array of N std::byte” (17.2.1 [cstddef.syn]), that array provides storage for the created object if:
  • the lifetime of e has begun and not ended, and
  • the storage for the new object fits entirely within e, and
  • e is not and is not nested within an object of type similar (7.3.6 [conv.qual]) to T that is within its lifetime, and
  • there is no array object that satisfies these constraints nested within e.
Date: 2023-06-08.00:00:00

(From thread beginning here.)

Consider:

  #include <new>

  struct A { unsigned char buf[1]; };
  static_assert(sizeof(A) == 1); // A can fit within A::buf

  int main()
  {
    A x{};
    new (x.buf) A{};
  }

A::buf provides storage for another A object. Thus, there are now two objects of type A within lifetime, which is inconsistent with the goal expressed by 6.7.2 [intro.object] paragraph 9.

History
Date User Action Args
2023-06-09 07:14:31adminsetmessages: + msg7308
2023-06-08 00:00:00admincreate