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

2024-12-19


2897. Copying potentially-overlapping union subobjects

Section: 11.4.6  [class.copy.assign]     Status: open     Submitter: Jiang An     Date: 2024-05-30

(From submission #542.)

Consider:

  union U {};
  struct S {
    U u [[no_unique_address]];
    char c;
  };
  S s;
  s.c = 1;
  s.u = U();   // #1

As specified, the move-assignment at #1 might disturb the value of s.c.

Suggested resolution:

(Note that the value representation of a union arguably depends on which of its members is active.)

  1. Change in 11.4.5.3 [class.copy.ctor] paragraph 15 as follows:

    The implicitly-defined copy/move constructor for a union X copies the object value representation (6.8.1 [basic.types.general]) of X. For each object nested within (6.7.2 [intro.object]) the object that is the source of the copy, a corresponding object o nested within the destination is identified (if the object is a subobject) or created (otherwise), and the lifetime of o begins before the copy is performed.
  2. Change in 11.4.6 [class.copy.assign] paragraph 13 as follows:

    The implicitly-defined copy/move assignment operator for a union X copies the object value representation (6.8.1 [basic.types.general]) of X. If the source and destination of the assignment are not the same object, then for each object nested within (6.7.2 [intro.object]) the object that is the source of the copy, a corresponding object o nested within the destination is created, and the lifetime of o begins before the copy is performed.