Skip to content

[class.copy.assign] Trivial assignment operators can skip member subobjects #759

@frederick-vs-ja

Description

@frederick-vs-ja

Full name of submitter (unless configured in github; will be published with the issue): Jiang An

Reference (section label): [class.copy.assign]

Link to reflector thread (if any):

Issue description:

It's possible to assign between objects of a derived class via a trivial assignment operator of the base class. As a result, it's possible to synthesize a defaulted and trivial assignment operator that skip member subobjects from such a derived class.

It seems that no implementation supports such skipping per llvm/llvm-project#36386. All investigated implementations synthesize trivial assignment operators which don't skip member subobjects.

E.g.

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;
};

Q qa, qb;
int main() {
  qb.d.b0 = 42;
  qb = qa;
  if (qb.d.b0 != 42)
    abort(); // selected by all known implementations
}

It's questionable whether the skipping should even exist. Perhaps we should either make the synthesized assignment operator non-trivial or non-skipping.

Suggested resolution:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions