Title
Trivial assignment can skip member subobjects
Status
ready
Section
11.4.6 [class.copy.assign]
Submitter
Jiang An

Created on 2025-09-05.00:00:00 last changed 1 month ago

Messages

Date: 2025-11-05.18:50:44

Proposed resolution (approved 2025-11-05):

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

    A copy/move constructor for class X is trivial if it is not user-provided and if
    • class X has no virtual functions (11.7.3 [class.virtual]) and no virtual base classes (11.7.2 [class.mi]), and
    • the constructor selected to copy/move each direct base class subobject is a direct member of that base class and is trivial, and
    • for each non-static data member of X that is of class type (or array thereof), the constructor selected to copy/move that member is trivial;
    otherwise the copy/move constructor is non-trivial.
  2. Change in 11.4.6 [class.copy.assign] paragraph 9 as follows:

    A copy/move assignment operator for class X is trivial if it is not user-provided and if
    • class X has no virtual functions (11.7.3 [class.virtual]) and no virtual base classes (11.7.2 [class.mi]), and
    • the assignment operator selected to copy/move each direct base class subobject is a direct member of that base class and is trivial, and
    • for each non-static data member of X that is of class type (or array thereof), the assignment operator selected to copy/move that member is trivial;
    otherwise the copy/move assignment operator is non-trivial.
Date: 2025-11-05.18:50:44

(From submission #759.)

Consider:

  struct B0 { int b0; };

  struct B {
    B &operator=(const B &) = default;
    int x;
  };

  struct D : B0, B {
    using B::operator=;
  private:
    D &operator=(const D &) && = default;
  };

  struct Q {
    Q &operator=(const Q &) = default;
    D d;
  };

According to the rules, Q::operator= is trivial, but it does not copy the d.B0::b0 member of Q. Implementations disagree and copy that member regardless.

We can make Q::operator= non-trivial or deleted. We can also make that assignment operator copy all subobjects, even though overload resolution on the base classes clearly is at odds with that outcome.

History
Date User Action Args
2025-11-07 07:33:50adminsetstatus: tentatively ready -> ready
2025-11-05 18:50:44adminsetmessages: + msg8218
2025-11-05 18:50:44adminsetstatus: review -> tentatively ready
2025-11-05 13:55:49adminsetstatus: open -> review
2025-09-05 00:00:00admincreate