copy/move assignment
- A user-declared copy assignment operator X::operator= is a non-static non-template member function of class X with exactly one parameter of type X, X&, const X&, volatile X& or const volatile X&. This means that a template assignment operator or an assignment operator taking an rvalue reference parameter isnever a copy assignment operator, the presence of such an assignment operator does not suppress the implicit declaration of a copy assignment operator. Such assignment operators participate in overload resolution with other assignment operators, including copy assignment operators, and, if selected, will be used to assign an object.
- If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy assignment operator is defined as deleted; otherwise, it is defined as defaulted. The latter case is deprecated if the class has a user-declared copy constructor or a user-declared destructor.
- The implicitly declared copy assignment operator for a class X will have the form X& X::operator=(const X&) if
- each direct base class B of X has a copy assignment operator whose parameter is of type const B&, const volatile B& or B, and
- for all the non-static data members of X that are of a class type M (or array thereof), each such class type has a copy assignment operator whose parameter is of type const M&, const volatile M& or M
Otherwise, the implicitly-declared copy assignment operator will have the form X& X::operator=(X&)
- each direct base class B of X has a copy assignment operator whose parameter is of type const B&, const volatile B& or B, and
- A user-declared move assignment operator X::operator= is a non-static non-template member function of class X with exactly one parameter of typeX&&, const X&&, volatile X&&, or const volatile X&&.
- If the definition of a class X does not explicitly declare a move assignment operator, one will be implicitly declared as defaulted if and only if
- X does not have a user-declared copy constructor
- X does not have a user-declared move constructor
- X does not have a user-declared copy assignment operator, and
- X does not have a user-declared destructor
- X does not have a user-declared copy constructor
- A defaulted copy/move assignment operator for class X is defined as deleted if X has:
- a variant member with a non-trivial corresponding assignment operator and X is a union-like class, or
- a non-static data member of const non-class type (or array thereof), or
- a non-static data member of reference type, or
- a non-static data member of class type M (or array thereof) that cannot be copied/moved because overload resolution, as applied to M’s corresponding assignment operator, results in an ambiguity or a function that is deleted or inaccessible from the defaulted assignment operator, or
- a direct or virtual base class B that cannot be copied/moved because overload resolution, as applied to B’s corresponding assignment operator, results in an ambiguity or a function that is deleted or inaccessible from the defaulted assignment operator
- a variant member with a non-trivial corresponding assignment operator and X is a union-like class, or
- The implicitly-defined copy/move assignment operator for anon-union class X performs member wise copy/move assignment of its subobjects. The direct base classes of X are assigned first, in the order of their declaration in the base-specifier-list, and then the immediate non-static data members of X are assigned, in the order in which they were declared in the class definition. Let x be either the parameter of the function or, for the move operator, an xvalue referring to the parameter. Each subobject is assigned in the manner appropriate to its type:
- if the subobject is of class type, as if by a call to operator= with the subobject as the object expression and the corresponding subobject of x as a single function argument (as if by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);
- if the subobject is an array, each element is assigned, in the manner appropriate to the element type;
- if the subobject is of scalar type, the built-in assignment operator is used.
- if the subobject is of class type, as if by a call to operator= with the subobject as the object expression and the corresponding subobject of x as a single function argument (as if by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);
- It is unspecified whether subobjects representing virtual base classes are assigned more than once by the implicitly-defined copy assignment operator. Example:
struct V { };
struct A : virtual V { };
struct B : virtual V { };
struct C : B, A { };
It is unspecified whether the virtual base class subobject V is assigned twice by the implicitly-defined copy assignment operator for C. [Note: This does not apply to move assignment, as a defaulted move assignment operator is deleted if the class has virtual bases]. - The implicitly-defined copy assignment operator for a union X copies the object representation of X.