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

2024-12-06


2677. Replacing union subobjects

Section: 6.7.3  [basic.life]     Status: review     Submitter: Richard Smith     Date: 2022-12-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.

Suggested resolution [SUPERSEDED]:

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

... An object o1 is transparently replaceable by an object o2 if:

Additional notes (February, 2023)

The suggested resolution above does not handle the additional example in issue 2676.

Suggested resolution:

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

    Objects can contain other objects, called subobjects. A subobject can be a member subobject (11.4 [class.mem]), a base class subobject (11.7 [class.derived]), or an array element. An object that is not a subobject of any other object is called a complete object. If an object is created in storage associated with a member subobject or array element subobject e (which may or may not be within its lifetime), the created object is a subobject of e's containing object if:
    • the lifetime of e's containing object has begun and not ended, and
    • the storage for the new object exactly overlays the storage location associated with e, and
    • e is not a potentially-overlapping subobject, and
    • the new object is of the same type as e (ignoring cv-qualification).
    In this case, e and the created object are corresponding direct subobjects.
  2. Change in 6.7.3 [basic.life] paragraph 8 as follows:

    ... An object o1 is transparently replaceable by an object o2 if either
    • o1 and o2 are complete objects for which:
      • o1 is not const,
      • 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 or
      • o1 is not a const, complete object, and
      • neither o1 nor o2 is a potentially-overlapping subobject (6.7.2 [intro.object]), and
    • 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 (6.7.2 [intro.object]) for which:
      • the complete object of o1 is not const or
      • o1 is a mutable member subobject or a subobject thereof.