Title
Implicit change of active union member for anonymous union in union
Status
drwp
Section
11.5.1 [class.union.general]
Submitter
Richard Smith

Created on 2022-05-29.00:00:00 last changed 7 months ago

Messages

Date: 2023-06-20.19:34:52

Proposed resolution (approved by CWG 2023-06-17):

Change in 11.5.1 [class.union.general] paragraph 6 as follows:

In an assignment expression of the form E1 = E2 that uses either the built-in assignment operator (7.6.19 [expr.ass]) or a trivial assignment operator (11.4.6 [class.copy.assign]), for each element X of S(E1) and each anonymous union member X (11.5.2 [class.union.anon]) that is a member of a union and has such an element as an immediate subobject (recursively), if modification of X would have undefined behavior under 6.7.3 [basic.life], an object of the type of X is implicitly created in the nominated storage; no initialization is performed and the beginning of its lifetime is sequenced after the value computation of the left and right operands and before the assignment.

Editing note: Adding this rule into the definition of S would be more logical, but S(E) is a set of subexpressions of E and there is no form of expression that names an anonymous union member. Redefining S(E) to be a set of objects might be a better option.

Date: 2023-11-15.00:00:00

[Accepted as a DR at the November, 2023 meeting.]

Subclause 11.5.1 [class.union.general] paragraph 6 describes how union member subobjects are implicitly created by certain assignment operations that assign to union members. However, this description does not appear to properly handle the case of an anonymous union appearing within a union:

  union A {
    int x;
    union {
     int y;
    };
  };
  void f() {
    A a = {.x = 1};
    a.y = 2;
  }

Here, the expectation is that the assignment to a.y starts the lifetime of the anonymous union member subobject within A and also the int member subobject of the anonymous union member subobject. But the algorithm for computing S(a.y) determines that it is {a.y} and does not include the anonymous union member subobject.

History
Date User Action Args
2024-04-05 21:43:46adminsetstatus: dr -> drwp
2023-12-19 10:15:28adminsetstatus: ready -> dr
2023-11-10 14:27:11adminsetstatus: tentatively ready -> ready
2023-06-20 19:34:52adminsetstatus: open -> tentatively ready
2022-05-30 19:11:22adminsetmessages: + msg6839
2022-05-29 00:00:00admincreate