cpp/language/move assignment

A move assignment operator of class is a non-template  with the name  that takes exactly one parameter of type, , , or.

Explanation
@1@ Typical declaration of a move assignment operator. @2@ Forcing a move assignment operator to be generated by the compiler. @3@ Avoiding implicit move assignment.

The move assignment operator is called whenever it is selected by, e.g. when an object appears on the left-hand side of an assignment expression, where the right-hand side is an rvalue of the same or implicitly convertible type.

Move assignment operators typically "steal" the resources held by the argument (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, I/O streams, running threads, etc.), rather than make copies of them, and leave the argument in some valid but otherwise indeterminate state. For example, move-assigning from a std or from a std may result in the argument being left empty. This is not, however, a guarantee. A move assignment is less, not more restrictively defined than ordinary assignment; where ordinary assignment must leave two copies of data at completion, move assignment is required to leave only one.

Implicitly-declared move assignment operator
If no user-defined move assignment operators are provided for a class type, and all of the following is true: then the compiler will declare a move assignment operator as an member of its class with the signature.
 * there are no user-declared s;
 * there are no user-declared s;
 * there are no user-declared s;
 * there is no user-declared ,

A class can have multiple move assignment operators, e.g. both and. If some user-defined move assignment operators are present, the user may still force the generation of the implicitly declared move assignment operator with the keyword.

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

Because some assignment operator (move or copy) is always declared for any class, the base class assignment operator is always hidden. If a using-declaration 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 move assignment operator
The implicitly-declared or defaulted move assignment operator for class is defined as deleted if any of the following is true:
 * has a non-static data member that is ;
 * has a non-static data member of a reference type;
 * has a non-static data member or a direct base class that cannot be move-assigned (has deleted, inaccessible, or ambiguous move assignment operator).

A deleted implicitly-declared move assignment operator is ignored by.

Trivial move assignment operator
The move 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 move assignment operator selected for every direct base of is trivial;
 * the move assignment operator selected for every non-static class type (or array of class type) member of is trivial.

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

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

Implicitly-defined move assignment operator
If the implicitly-declared move 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 move assignment operator copies the object representation (as by std).

For non-union class types, the move assignment operator performs full member-wise move assignment of the object's direct bases and immediate non-static members, in their declaration order, using built-in assignment for the scalars, memberwise move-assignment for arrays, and move assignment operator for class types (called non-virtually).

As with copy assignment, it is unspecified whether virtual base class subobjects that are accessible through more than one path in the inheritance lattice, are assigned more than once by the implicitly-defined move assignment operator: