Title
Replacing a complete object having base subobjects
Status
open
Section
6.7.3 [basic.life]
Submitter
Richard Smith

Created on 2022-12-06.00:00:00 last changed 13 months ago

Messages

Date: 2023-02-15.00:00:00

Additional notes (February, 2023)

Consider this example:

  struct A {
   int n;
   char c;
   // tail padding
  };
  struct B {
   [[no_unique_address]] A a;
   char in_tail_padding[3];
  };

  B b;
  void f() {
   // Transparently replaces old member, potentially overwriting the data in the tail padding!
   new (&b.a) A{};
  }

The suggestions do not address this example.

Date: 2022-12-06.00:00:00

Base subobjects cannot be transparently replaced with complete objects, as specified in 6.7.3 [basic.life] bullet 8.4:

An object o1 is transparently replaceable by an object o2 if:
  • ...
  • neither o1 nor o2 is a potentially-overlapping subobject (6.7.2 [intro.object]), and
  • ...

However, that bullet is over-reaching, because it disallows:

  struct A { int n; };
  struct B : A {};
  B b;
  new (&b) B { {5} };  // New A base class does not transparently replace existing A base class due to /8.4.
  int k = b.n;  // UB: member n of A base class is outside its lifetime

See issue 2677 for a suggested resolution.

History
Date User Action Args
2023-02-27 18:03:20adminsetmessages: + msg7209
2022-12-06 00:00:00admincreate