General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
A move assignment operator is a non-templatenon-static member function with the nameoperator= that can be called with an argument of the same class type and copies the content of the argument, possibly mutating the argument.
Contents |
For the formal move assignment operator syntax, seefunction declaration. The syntax list below only demonstrates a subset of all valid move assignment operator syntaxes.
return-typeoperator=( parameter-list ); | (1) | ||||||||
return-typeoperator=( parameter-list ) function-body | (2) | ||||||||
return-typeoperator=( parameter-list-no-default ) = default; | (3) | ||||||||
return-typeoperator=( parameter-list ) = delete; | (4) | ||||||||
return-typeclass-name :: operator=( parameter-list ) function-body | (5) | ||||||||
return-typeclass-name :: operator=( parameter-list-no-default ) = default; | (6) | ||||||||
class-name | - | the class whose move assignment operator is being declared, the class type is given asT in the descriptions below |
parameter-list | - | aparameter list of only one parameter, which is of typeT&& ,const T&&,volatile T&& orconstvolatile T&& |
parameter-list-no-default | - | aparameter list of only one parameter, which is of typeT&& ,const T&&,volatile T&& orconstvolatile T&& and does not have a default argument |
function-body | - | thefunction body of the move assignment operator |
return-type | - | any type, butT& is favored in order to be consistent with scala types |
struct X{ X& operator=(X&& other);// move assignment operator// X operator=(const X other); // Error: incorrect parameter type}; union Y{// move assignment operators can have syntaxes not listed above,// as long as they follow the general function declaration syntax// and do not viloate the restrictions listed aboveauto operator=(Y&& other)-> Y&;// OK: trailing return type Y& operator=(this Y&& self, Y& other);// OK: explicit object parameter// Y& operator=(Y&&, int num = 1); // Error: has other non-object parameters};
The move assignment operator is called whenever it is selected byoverload resolution, 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 transfer the resources held by the argument (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, thread handles, etc.), rather than make copies of them, and leave the argument in some valid but otherwise indeterminate state. Since move assignment doesn’t change the lifetime of the argument, the destructor will typically be called on the argument at a later point. For example, move-assigning from astd::string or from astd::vector may result in the argument being left empty. 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.
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 aninlinepublic member of its class with the signatureT& T::operator=(T&&).
A class can have multiple move assignment operators, e.g. bothT& T::operator=(const T&&) andT& T::operator=(T&&). 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 keyworddefault
.
The implicitly-declared move assignment operator has an exception specification as described indynamic exception specification(until C++17)noexcept specification(since C++17).
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.
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 ifodr-used orneeded for constant evaluation(since C++14).
For union types, the implicitly-defined move assignment operator copies the object representation (as bystd::memmove).
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).
The implicitly-defined move assignment operator for a class
| (since C++14) (until C++23) |
The implicitly-defined move assignment operator for a class | (since C++23) |
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:
struct V{ V& operator=(V&& other){// this may be called once or twice// if called twice, 'other' is the just-moved-from V subobjectreturn*this;}}; struct A:virtual V{};// operator= calls V::operator=struct B:virtual V{};// operator= calls V::operator=struct C: B, A{};// operator= calls B::operator=, then A::operator=// but they may only call V::operator= once int main(){ C c1, c2; c2= std::move(c1);}
The implicitly-declared or defaulted move assignment operator for classT
is defined as deleted if any of the following conditions is satisfied:
T
has a non-static data member of a const-qualified non-class type (or possibly multi-dimensional array thereof).T
has a non-static data member of a reference type.T
has apotentially constructed subobject of class typeM
(or possibly multi-dimensional array thereof) such that the overload resolution as applied to findM
's move assignment operatorA deleted implicitly-declared move assignment operator is ignored byoverload resolution.
The move assignment operator for classT
is trivial if all of the following is true:
T
has no virtual member functions;T
has no virtual base classes;T
is trivial;T
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 bystd::memmove. All data types compatible with the C language are trivially move-assignable.
A move assignment operator is eligible if it is not deleted. | (until C++20) |
A move assignment operator is eligible if all following conditions are satisfied:
| (since C++20) |
Triviality of eligible move assignment operators determines whether the class is atrivially copyable type.
If both copy and move assignment operators are provided, overload resolution selects the move assignment if the argument is anrvalue (either aprvalue such as a nameless temporary or anxvalue such as the result ofstd::move), and selects the copy assignment if the argument is anlvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.
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 (same applies tocopy assignment).
Seeassignment operator overloading for additional detail on the expected behavior of a user-defined move-assignment operator.
#include <iostream>#include <string>#include <utility> struct A{std::string s; A(): s("test"){} A(const A& o): s(o.s){std::cout<<"move failed!\n";} A(A&& o): s(std::move(o.s)){} A& operator=(const A& other){ s= other.s;std::cout<<"copy assigned\n";return*this;} A& operator=(A&& other){ s= std::move(other.s);std::cout<<"move assigned\n";return*this;}}; A f(A a){return a;} struct B: A{std::string s2;int n;// implicit move assignment operator B& B::operator=(B&&)// calls A's move assignment operator// calls s2's move assignment operator// and makes a bitwise copy of n}; struct C: B{ ~C(){}// destructor prevents implicit move assignment}; struct D: B{ D(){} ~D(){}// destructor would prevent implicit move assignment D& operator=(D&&)=default;// force a move assignment anyway}; int main(){ A a1, a2;std::cout<<"Trying to move-assign A from rvalue temporary\n"; a1= f(A());// move-assignment from rvalue temporarystd::cout<<"Trying to move-assign A from xvalue\n"; a2= std::move(a1);// move-assignment from xvalue std::cout<<"\nTrying to move-assign B\n"; B b1, b2;std::cout<<"Before move, b1.s =\""<< b1.s<<"\"\n"; b2= std::move(b1);// calls implicit move assignmentstd::cout<<"After move, b1.s =\""<< b1.s<<"\"\n"; std::cout<<"\nTrying to move-assign C\n"; C c1, c2; c2= std::move(c1);// calls the copy assignment operator std::cout<<"\nTrying to move-assign D\n"; D d1, d2; d2= std::move(d1);}
Output:
Trying to move-assign A from rvalue temporarymove assignedTrying to move-assign A from xvaluemove assigned Trying to move-assign BBefore move, b1.s = "test"move assignedAfter move, b1.s = "" Trying to move-assign Ccopy assigned Trying to move-assign Dmove assigned
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 1353 | C++11 | the conditions where defaulted move assignment operators are defined as deleted did not consider multi-dimensional array types | consider these types |
CWG 1402 | C++11 | a defaulted move assignment operator that would call a non-trivial copy assignment operator was deleted; a defaulted move assignment operator that is deleted still participated in overload resolution | allows call to such copy assignment operator; made ignored in overload resolution |
CWG 1806 | C++11 | specification for a defaulted move assignment operator involving a virtual base class was missing | added |
CWG 2094 | C++11 | a volatile subobject made of a defaulted move assignment operator non-trivial (CWG issue 496) | triviality not affected |
CWG 2180 | C++11 | a defaulted move assignment operator for classT was not defined as deleted if T is abstract and hasnon-move-assignable direct virtual base classes | the operator is defined as deleted in this case |
CWG 2595 | C++20 | a move assignment operator was not eligible if there is another move assignment operator which is more constrained but does not satisfy its associated constraints | it can be eligible in this case |
CWG 2690 | C++11 | the implicitly-defined move assignment operator for union types did not copy the object representation | they copy the object representation |