Replacing union subobjects
6.7.3 [basic.life]
Richard Smith

Created on 2022-12-06.00:00:00 last changed 3 weeks ago


Date: 2023-01-07.20:23:15

Suggested resolution:

Change in 6.7.3 [basic.life] paragraph 8 as follows:

... An object o1 is transparently replaceable by an object o2 if:
  • o2 is a complete object and:
    • the storage that o2 occupies exactly overlays the storage that o1 occupied, and
    • o1 and o2 are of the same type (ignoring the top-level cv-qualifiers), and
    • o1 is not a const, complete object, and
    • neither o1 nor o2 is not a potentially-overlapping subobject (6.7.2 [intro.object]), and or
  • either o1 and o2 are both complete objects, or o1 and o2 are corresponding direct subobjects of objects p1 and p2, respectively, and p1 is transparently replaceable by p2.
Date: 2023-01-07.20:24:06

The resolution to NB comment US 041 (C++20 CD) does not seem to have fully addressed the original issue, allowing:

  union U { int i, j; };
  U u;
  new (&u) U{.i = 5};
  int k = u.j;  // OK! New U::i transparently replaces existing u.j!

The suggestion is to allow a newly-created complete object to transparently replace any object of the same type in the same storage, except for a potentially-overlapping subobject or a const complete object, and to allow a newly-created subobject to transparently replace only a corresponding subobject of an existing object.

Date User Action Args
2023-01-07 20:23:15adminsetmessages: + msg7125
2022-12-06 00:00:00admincreate