cpp/language/copy assignment

A copy assignment operator of class is a non-template  with the name  that takes exactly one parameter (that isn't an ) of type, , , , or. For a type to be, it must have a public copy assignment operator.

Explanation
@1@ Typical declaration of a copy assignment operator when is used. @2@ Typical declaration of a copy assignment operator when copy-and-swap idiom is not used. @3@ Forcing a copy assignment operator to be generated by the compiler. @4@ Avoiding implicit copy assignment.

The copy assignment operator is called whenever selected by, e.g. when an object appears on the left side of an assignment expression.

Implicitly-declared copy assignment operator
If no user-defined copy assignment operators are provided for a class type (,, or ), the compiler will always declare one as an inline public member of the class. This implicitly-declared copy assignment operator has the form if all of the following is true: Otherwise the implicitly-declared copy assignment operator is declared as. (Note that due to these rules, the implicitly-declared copy assignment operator cannot bind to a volatile lvalue argument.)
 * each direct base of  has a copy assignment operator whose parameters are  or  or ;
 * each non-static data member of  of class type or array of class type has a copy assignment operator whose parameters are  or  or.

A class can have multiple copy assignment operators, e.g. both and.

The implicitly-declared (or defaulted on its first declaration) copy assignment operator has an exception specification as described in

Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration.

Deleted implicitly-declared copy assignment operator
An implicitly-declared copy assignment operator for class is defined as deleted if any of the following is true: Otherwise, it is defined as defaulted.
 * has a user-declared move constructor;
 * has a user-declared move assignment operator.

A defaulted copy assignment operator for class is defined as deleted if any of the following is true:
 * has a non-static data member of a const-qualified non-class type (or array thereof);
 * has a non-static data member of a reference type;
 * has a non-static data member or a direct base class that cannot be copy-assigned (overload resolution for the copy assignment fails, or selects a deleted or inaccessible function);
 * is a, and has a variant member whose corresponding assignment operator is non-trivial.

Trivial copy assignment operator
The copy assignment operator for class is trivial if all of the following is true:
 * it is not user-provided (meaning, it is implicitly-defined or defaulted);
 * has no virtual member functions;
 * has no virtual base classes;
 * the copy assignment operator selected for every direct base of is trivial;
 * the copy assignment operator selected for every non-static class type (or array of class type) member of is trivial.

A trivial copy assignment operator makes a copy of the object representation as if by std. All data types compatible with the C language (POD types) are trivially copy-assignable.

Eligible copy assignment operator
Triviality of eligible copy assignment operators determines whether the class is a trivially copyable type.

Implicitly-defined copy assignment operator
If the implicitly-declared copy assignment operator is neither deleted nor trivial, it is defined (that is, a function body is generated and compiled) by the compiler if. For union types, the implicitly-defined copy assignment copies the object representation (as by std). For non-union class types ( and ), the operator performs member-wise copy assignment of the object's bases and non-static members, in their initialization order, using built-in assignment for the scalars and copy assignment operator for class types.