Title
Trivial assignment can skip member subobjects
Status
open
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-09-05.00:00:00

(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.

Possible resolution

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.
History
Date User Action Args
2025-09-05 00:00:00admincreate