Title
Replacing union subobjects
Status
review
Section
6.7.4 [basic.life]
Submitter
Richard Smith

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

Messages

Date: 2023-02-23.21:39:16

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.4 [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.
Date: 2023-02-15.00:00:00

Additional notes (February, 2023)

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

Date: 2023-02-23.21:27:52

Suggested resolution [SUPERSEDED]:

Change in 6.7.4 [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.

History
Date User Action Args
2023-03-03 21:06:51adminsetstatus: open -> review
2023-02-23 21:27:05adminsetmessages: + msg7207
2023-02-23 21:27:05adminsetmessages: + msg7206
2023-01-07 20:23:15adminsetmessages: + msg7125
2022-12-06 00:00:00admincreate