Namespaces
Variants

cpp/language/copy assignment: Difference between revisions

From cppreference.com
Radix (talk | contribs)
m Syntax: Drop whitesapce from {{ttb}}.
Space Mission (talk | contribs)
m Notes: shorter
 
(47 intermediate revisions by 14 users not shown)
Line 2: Line 2:
{{cpp/language/classes/navbar}}
{{cpp/language/classes/navbar}}


A copy assignment operator of class {{tt|T}} is a non-template {{rlp|member functions|non-static member function}} with the name {{c|operator{{=}}}} that takes exactly one parameter of type {{c|T}}, {{c|T&}}, {{c|const T&}}, {{c|volatile T&}}, or {{c|const volatile T&}}. For a type to be {{named req|CopyAssignable}}, it must have a public copy assignment operator.
A copy assignment operator is a non-template {{rlp|member functions|non-static member function}} with the name {{c|operator=}} that of type .


===Syntax===
===Syntax===
{{sdsc begin}}
{{sdsc begin}}
{{sdsc |num=1| {{spar|class_name}} {{ttb|&}} {{spar|class_name}} {{ttb|::}} {{ttb|operator{{=}}}} ( {{spar|class_name}} ) }}  
{{sdsc|num=1|{{spar|}} {{ttb|}}{{spar |}}{{ttb|
{{sdsc |num=2| {{spar|class_name}} {{ttb|&}} {{spar|class_name}} {{ttb|::}} {{ttb|operator{{=}}}} ( const {{spar|class_name}} {{tt|&}} ) }}  
}} {{ttb|operator{{}}}} {{spar|}}}}
{{sdsc |num=3|notes={{mark since c++11}}| {{spar|class_name}} {{ttb|&}} {{spar|class_name}} {{ttb|::}} {{ttb|operator{{=}}}} ( const {{spar|class_name}} {{tt|&}} ) {{=}} {{c|default;}} }}
{{sdsc|num=|{{spar|}} {{ttb|}}{{spar |}}{{ttb|}}|{{}}}}
{{sdsc |num=4|notes={{mark since c++11}}| {{spar|class_name}} {{ttb|&}} {{spar|class_name}} {{ttb|::}} {{ttb|operator{{=}}}} ( const {{spar|class_name}} {{tt|&}} ) {{=}} {{c|delete;}} }}
{{spar|}} {{|}}}}{{|= |notes={{mark since c++11}}
{{|{{|}} {{spar |}}{{ttb|::}}{{ttb|operator=}}{{spar |}}{{|)}} {{|}}}}
{{sdsc|num=|{{spar|}} {{spar |}}{{ttb|::}}{{ttb|operator=}}{{spar |}}{{|) = }}{{c}}}}
{{sdsc end}}
{{sdsc end}}


===Explanation===
===Explanation===
# Typical declaration of a copy assignment operator when [[cpp/language/operators#Assignment_operator|copy-and-swap idiom]] can be used.
of a copy assignment operator .
# Typical declaration of a copy assignment operator when copy-and-swap idiom cannot be used (non-swappable type or degraded performance).
of a copy assignment operator
# Forcing a copy assignment operator to be generated by the compiler.
copy -
# Avoiding implicit copy assignment.
.
a copy assignment operator the .
copy assignment .


The copy assignment operator is called whenever selected by [[cpp/language/overload_resolution|overload resolution]], e.g. when an object appears on the left side of an assignment expression.
 
 
The copy assignment operator is called whenever selected by |overload resolution, e.g. when an object appears on the left side of an assignment expression.


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


A class can have multiple copy assignment operators, e.g. both {{c|1=T& T::operator=(const T&)}} and {{c|1=T& T::operator=(T)}}. {{rev inl|since=c++11|If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword {{tt|default}}.}}
, the implicitlydeclared copy assignment operator {{|}} .


The implicitly-declared (or defaulted on its first declaration) copy assignment operator has an exception specification as described in {{rev inl|until=c++17|{{rlp|except_spec|dynamic exception specification}}}}{{rev inl|since=c++17|{{rlp|noexcept_spec|exception specification}}}}
 
The implicitly-declared (or defaulted on its first declaration) copy assignment operator has an exception specification as described in {{rev inl|until=c++17|{{rlp||dynamic exception specification}}}}{{rev inl|since=c++17|{{rlp||specification}}}}


Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a {{rlp|using declaration#In class definition|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.
Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a {{rlp|using declaration#In class definition|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 copy assignment operator===
===
A implicitly-declared copy assignment operator for class {{tt|T}} is defined as ''deleted'' if any of the following is true:
implicitly-declared copy assignment operator =
* {{tt|T}} has a user-declared move constructor;
 
* {{tt|T}} has a user-declared move assignment operator.
==
Otherwise, it is defined as defaulted.
implicitly-copy assignment operator for class {{tt|T}} is if
the is
* {{tt|T}} a
-
 
 
{{tt|T}} has a user-declared .


A defaulted copy assignment operator for class {{tt|T}} is defined as ''deleted'' if any of the following is true:
* {{tt|T}} has a non-static data member of non-class type (or array thereof) that is {{c|const}};
defaultedcopy assignment operator for class {{tt|T}} is defined as deletedif any of the following is :
* {{tt|T}} has a non-static data member of a reference type;
* {{tt|T}} has a non-static data member of non-class type (or array thereof)
* {{tt|T}} has a non-static data member or a direct or virtual base class that cannot be copy-assigned (overload resolution for the copy assignment fails, or selects a deleted or inaccessible function);
* {{tt|T}} has a non-static data member of a reference type
* {{tt|T}} is a [[cpp/language/union#Union-like classes|union-like class]], and has a variant member whose corresponding assignment operator is non-trivial.
* {{tt|T}} has a -that overload resolution copy assignment
, or
* {{|union#Union-like classes|
 
-class a assignment operator.


===Trivial copy assignment operator===
===Trivial copy assignment operator===
The copy assignment operator for class {{tt|T}} is trivial if all of the following is true:
The copy assignment operator for class {{tt|T}} is trivial if all of the following is true:
* it is not user-provided (meaning, it is implicitly-defined or defaulted){{rev inl|until=c++14|{{mark unreviewed dr|CWG|2171}}, and if it is defaulted, its signature is the same as implicitly-defined}};
* it is not user-provided (meaning, it is implicitly-defined or defaulted);
* {{tt|T}} has no virtual member functions;
* {{tt|T}} has no virtual member functions;
* {{tt|T}} has no virtual base classes;
* {{tt|T}} has no virtual base classes;
* the copy assignment operator selected for every direct base of {{tt|T}} is trivial;
* the copy assignment operator selected for every direct base of {{tt|T}} is trivial;
* the copy assignment operator selected for every non-static class type (or array of class type) member of {{tt|T}} is trivial;
* the copy assignment operator selected for every non-static class type (or array of class type) member of {{tt|T}} is trivial.
{{rev begin}}
 
{{rev | since=c++14 |
* {{tt|T}} has no non-static data members of {{rlp|cv|volatile}}-qualified type.
}}
{{rev end}}
A trivial copy assignment operator makes a copy of the object representation as if by {{lc|std::memmove}}. All data types compatible with the C language (POD types) are trivially copy-assignable.
A trivial copy assignment operator makes a copy of the object representation as if by {{lc|std::memmove}}. All data types compatible with the C language (POD types) are trivially copy-assignable.


===Implicitly-defined copy assignment operator===
===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 {{rlp|definition#ODR-use|odr-used}}. For {{c|union}} types, the implicitly-defined copy assignment copies the object representation (as by {{lc|std::memmove}}). For non-union class types ({{c|class}} and {{c|struct}}), 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.
{{|copy assignment }}


The generation of the implicitly-defined copy assignment operator is deprecated{{mark since c++11}} if {{tt|T}} has a user-declared destructor or user-declared copy constructor.
of copy assignment is |