Difference between revisions of "cpp/utility/pair/operator="
Jhcarl0814 (Talk | contribs) m (fix comment in code) |
Andreas Krug (Talk | contribs) m (<cstddef> for std::size_t, "" -> '', fmt) |
||
(13 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/utility/pair/title | operator{{=}}}} | + | {{cpp/utility/pair/title|operator{{=}}}} |
{{cpp/utility/pair/navbar}} | {{cpp/utility/pair/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
Line 7: | Line 7: | ||
constexpr pair& operator=( const pair& other ); | constexpr pair& operator=( const pair& other ); | ||
}} | }} | ||
− | {{dcl | + | {{dcl|num=2 |
− | | | + | |since=c++23|1= |
+ | constexpr const pair& operator=( const pair& other ) const; | ||
+ | }} | ||
+ | {{dcl rev multi|num=3 | ||
+ | |dcl1= | ||
template< class U1, class U2 > | template< class U1, class U2 > | ||
− | pair& operator=( const pair<U1,U2>& other ); | + | pair& operator=( const pair<U1, U2>& other ); |
|since2=c++20|dcl2= | |since2=c++20|dcl2= | ||
template< class U1, class U2 > | template< class U1, class U2 > | ||
− | constexpr pair& operator=( const pair<U1,U2>& other ); | + | constexpr pair& operator=( const pair<U1, U2>& other ); |
}} | }} | ||
− | {{dcl rev multi|num= | + | {{dcl|num=4 |
+ | |since=c++23|1= | ||
+ | template< class U1, class U2 > | ||
+ | constexpr const pair& operator=( const pair<U1, U2>& other ) const; | ||
+ | }} | ||
+ | {{dcl rev multi|num=5 | ||
+ | |since1=c++11|dcl1= | ||
pair& operator=( pair&& other ) noexcept(/* see below */); | pair& operator=( pair&& other ) noexcept(/* see below */); | ||
|since2=c++20|dcl2= | |since2=c++20|dcl2= | ||
constexpr pair& operator=( pair&& other ) noexcept(/* see below */); | constexpr pair& operator=( pair&& other ) noexcept(/* see below */); | ||
}} | }} | ||
− | {{dcl rev multi|num= | + | {{dcl|num=6 |
+ | |since=c++23|1= | ||
+ | constexpr const pair& operator=( pair&& other ) const; | ||
+ | }} | ||
+ | {{dcl rev multi|num=7|since1=c++11|dcl1= | ||
template< class U1, class U2 > | template< class U1, class U2 > | ||
− | pair& operator=( pair<U1,U2>&& | + | pair& operator=( pair<U1, U2>&& p ); |
|since2=c++20|dcl2= | |since2=c++20|dcl2= | ||
template< class U1, class U2 > | template< class U1, class U2 > | ||
− | constexpr pair& operator=( pair<U1,U2>&& | + | constexpr pair& operator=( pair<U1, U2>&& p ); |
+ | }} | ||
+ | {{dcl|num=8 | ||
+ | |since=c++23|1= | ||
+ | template< class U1, class U2 > | ||
+ | constexpr const pair& operator=( pair<U1, U2>&& p ) const; | ||
+ | }} | ||
+ | {{dcl|num=9|since=c++23|1= | ||
+ | template< pair-like P > | ||
+ | constexpr pair& operator=( P&& u ); | ||
+ | }} | ||
+ | {{dcl|num=10|since=c++23|1= | ||
+ | template< pair-like P > | ||
+ | constexpr const pair& operator=( P&& u ) const; | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Line 31: | Line 58: | ||
Replaces the contents of the pair. | Replaces the contents of the pair. | ||
− | @1@ Copy assignment operator. Replaces the contents with a copy of the contents of other. | + | @1@ Copy assignment operator. Replaces the contents with a copy of the contents of {{c|other}}. |
{{rrev multi|until1=c++11|rev1= | {{rrev multi|until1=c++11|rev1= | ||
− | + | The assignment operator is implicitly declared. Using this assignment operator makes the program ill-formed if either {{tt|T1}} or {{tt|T2}} is a const-qualified type, or a reference type, or a class type with an inaccessible copy assignment operator, or an array type of such class. | |
|rev2= | |rev2= | ||
− | + | This overload is defined as deleted if either {{c|std::is_copy_assignable<T1>::value}} or {{c|std::is_copy_assignable<T2>::value}} is {{c|false}}. | |
}} | }} | ||
− | @2@ | + | @2@ Copy assignment operator for const-qualified operand. |
− | + | @@ {{cpp/enable if|{{c|std::is_copy_assignable_v<const T1>}} and {{c|std::is_copy_assignable_v<const T2>}} are both {{c|true}}}}. | |
− | @3@ Move assignment operator. Replaces the contents with those of {{ | + | @3@ Assigns {{c|other.first}} to {{tt|first}} and {{c|other.second}} to {{tt|second}}. |
− | + | {{rrev|since=c++11| | |
+ | {{cpp/enable if|{{c|std::is_assignable<T1&, const U1&>::value}} and {{c|std::is_assignable<T2&, const U2&>::value}} are both {{c|true}}}}. | ||
+ | }} | ||
+ | |||
+ | @4@ Assigns {{c|other.first}} to {{tt|first}} and {{c|other.second}} to {{tt|second}}. | ||
+ | @@ {{cpp/enable if|{{c|std::is_assignable_v<const T1&, const U1&>}} and {{c|std::is_assignable_v<const T2&, const U2&>}} are both {{c|true}}}}. | ||
+ | |||
+ | @5@ Move assignment operator. Replaces the contents with those of {{c|other}} using move semantics. | ||
+ | @@ {{cpp/enable if|{{c|std::is_move_assignable<T1>::value}} and {{c|std::is_move_assignable<T2>::value}} are both {{c|true}}}}. | ||
+ | |||
+ | @6@ Move assignment operator for const-qualified operand. | ||
+ | @@ {{cpp/enable if|{{c|std::is_assignable_v<const T1&, T1>}} and {{c|std::is_assignable_v<const T2&, T2>}} are both {{c|true}}}}. | ||
+ | |||
+ | @7@ Assigns {{c|std::forward<U1>(p.first)}} to {{tt|first}} and {{c|std::forward<U2>(p.second)}} to {{tt|second}}. | ||
+ | @@ {{cpp/enable if|{{c|std::is_assignable<T1&, U1>::value}} and {{c|std::is_assignable<T2&, U2>::value}} are both {{c|true}}}}. | ||
+ | |||
+ | @8@ Assigns {{c|std::forward<U1>(p.first)}} to {{tt|first}} and {{c|std::forward<U2>(p.second)}} to {{tt|second}}. | ||
+ | @@ {{cpp/enable if|{{c|std::is_assignable_v<const T1&, U1>}} and {{c|std::is_assignable_v<const T2&, U2>}} are both {{c|true}}}}. | ||
+ | |||
+ | @9@ Assigns {{c|std::get<0>(std::forward<P>(u))}} to {{tt|first}} and {{c|std::get<1>(std::forward<P>(u))}} to {{tt|second}}. | ||
+ | @@ {{cpp/enable if| | ||
+ | * {{c|std::same_as<std::remove_cvref_t<P>, std::pair>}} is {{c|false}}, | ||
+ | * {{c/core|std::remove_cvref_t<P>}} is not a specialization of {{lc|std::ranges::subrange}}, | ||
+ | * {{c|std::is_assignable_v<T1&, decltype(std::get<0>(std::forward<P>(p)))>}} is {{c|true}}, and | ||
+ | * {{c|std::is_assignable_v<T1&, decltype(std::get<1>(std::forward<P>(p)))>}} is {{c|true}}.}} | ||
− | @ | + | @10@ Assigns {{c|std::get<0>(std::forward<P>(u))}} to {{tt|first}} and {{c|std::get<1>(std::forward<P>(u))}} to {{tt|second}}. |
− | + | @@ {{cpp/enable if| | |
+ | * {{c|std::same_as<std::remove_cvref_t<P>, std::pair>}} is {{c|false}}, | ||
+ | * {{c/core|std::remove_cvref_t<P>}} is not a specialization of {{lc|std::ranges::subrange}}, | ||
+ | * {{c|std::is_assignable_v<const T1&, decltype(std::get<0>(std::forward<P>(p)))>}} is {{c|true}}, and | ||
+ | * {{c|std::is_assignable_v<const T1&, decltype(std::get<1>(std::forward<P>(p)))>}} is {{c|true}}.}} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | other | pair of values to replace the contents of this pair }} | + | {{par|other|pair of values to replace the contents of this pair}} |
− | {{par end}} | + | {{par|p|pair of values of possibly different types to replace the contents of this pair}} |
+ | {{par|u|{{lti|cpp/utility/tuple/tuple-like|pair-like}} object of values to replace the contents of this pair}} | ||
+ | {{par hreq}} | ||
+ | {{par req|{{tt|T1}} must meet the requirements of {{named req|CopyAssignable}} from {{tt|U1}}. {{mark until c++11}}}} | ||
+ | {{par req|{{tt|T2}} must meet the requirements of {{named req|CopyAssignable}} from {{tt|U2}}. {{mark until c++11}}}} | ||
+ | {{par end}} | ||
===Return value=== | ===Return value=== | ||
Line 56: | Line 116: | ||
===Exceptions=== | ===Exceptions=== | ||
− | @1- | + | @1-4@ {{cpp/impldef exception item}} |
− | @ | + | @5@{{noexcept| |
std::is_nothrow_move_assignable<T1>::value && | std::is_nothrow_move_assignable<T1>::value && | ||
std::is_nothrow_move_assignable<T2>::value | std::is_nothrow_move_assignable<T2>::value | ||
}} | }} | ||
− | @ | + | @6-10@ {{cpp/impldef exception item}} |
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
+ | #include <cstddef> | ||
#include <iomanip> | #include <iomanip> | ||
#include <iostream> | #include <iostream> | ||
Line 71: | Line 132: | ||
#include <vector> | #include <vector> | ||
− | template <class Os, class T> | + | template<class Os, class T> |
− | Os& operator<<(Os& os, const std::vector<T>& v) { | + | Os& operator<<(Os& os, const std::vector<T>& v) |
− | os << | + | { |
+ | os << '{'; | ||
for (std::size_t t = 0; t != v.size(); ++t) | for (std::size_t t = 0; t != v.size(); ++t) | ||
− | os << v[t] << (t+1 < v.size() ? "," : ""); | + | os << v[t] << (t + 1 < v.size() ? ", " : ""); |
− | return os << | + | return os << '}'; |
} | } | ||
− | template <class Os, class U1, class U2> | + | template<class Os, class U1, class U2> |
− | Os& operator<<(Os& os, const std::pair<U1, U2>& pair) { | + | Os& operator<<(Os& os, const std::pair<U1, U2>& pair) |
− | return os << | + | { |
+ | return os << '{' << pair.first << ", " << pair.second << '}'; | ||
} | } | ||
int main() | int main() | ||
{ | { | ||
− | std::pair<int, std::vector<int>> p{ 1, {2} }, q{ 2, {5,6} }; | + | std::pair<int, std::vector<int>> p{1, {2}<!---->}, q{2, {5, 6}<!---->}; |
− | + | ||
− | p = q; // (1) operator=( const pair& other ); | + | p = q; // (1) operator=(const pair& other); |
− | std::cout << std::setw( | + | std::cout << std::setw(23) << std::left |
− | << "p = q;" << "p" << p << " | + | << "(1) p = q;" |
− | + | << "p: " << p << " q: " << q << '\n'; | |
− | + | ||
− | p = | + | std::pair<short, std::vector<int>> r{4, {7, 8, 9}<!---->}; |
− | std::cout << std::setw( | + | p = r; // (3) operator=(const pair<U1, U2>& other); |
− | << " | + | std::cout << std::setw(23) |
− | + | << "(3) p = r;" | |
− | std::pair< | + | << "p: " << p << " r: " << r << '\n'; |
− | p = | + | |
− | std::cout << std::setw( | + | p = std::pair<int, std::vector<int>>{3, {4}<!---->}; |
− | << "p = | + | p = std::move(q); // (5) operator=(pair&& other); |
− | + | std::cout << std::setw(23) | |
− | p = std::pair<int, std::vector<int>>{ 5, {6} }; | + | << "(5) p = std::move(q);" |
− | p = std::move(r); // ( | + | << "p: " << p << " q: " << q << '\n'; |
− | std::cout << std::setw( | + | |
− | << "p = std::move(r);" << "p" << p << " r" << r << '\n'; | + | p = std::pair<int, std::vector<int>>{5, {6}<!---->}; |
+ | p = std::move(r); // (7) operator=(pair<U1, U2>&& other); | ||
+ | std::cout << std::setw(23) | ||
+ | << "(7) p = std::move(r);" | ||
+ | << "p: " << p << " r: " << r << '\n'; | ||
} | } | ||
− | + | |output= | |
− | p = q; p:{ 2, {5,6} } | + | <nowiki> |
− | p = | + | (1) p = q; p: {2, {5, 6}} q: {2, {5, 6}} |
− | p = | + | (3) p = r; p: {4, {7, 8, 9}} r: {4, {7, 8, 9}} |
− | p = std::move(r); p:{ 4, {7,8,9} } r:{ 4, {} } | + | (5) p = std::move(q); p: {2, {5, 6}} q: {2, {}} |
+ | (7) p = std::move(r); p: {4, {7, 8, 9}} r: {4, {}} | ||
+ | </nowiki> | ||
}} | }} | ||
===Defect reports=== | ===Defect reports=== | ||
{{dr list begin}} | {{dr list begin}} | ||
+ | {{dr list item|wg=lwg|dr=885|std=C++98|before=missing heterogeneous copy assignment|after=added (as overload {{v|3}})}} | ||
{{dr list item|wg=lwg|dr=2729|std=C++11|before={{tt|1=pair::operator=}} was unconstrained and might<br>result in unnecessary undefined behavior|after=constrained}} | {{dr list item|wg=lwg|dr=2729|std=C++11|before={{tt|1=pair::operator=}} was unconstrained and might<br>result in unnecessary undefined behavior|after=constrained}} | ||
{{dr list end}} | {{dr list end}} | ||
Line 121: | Line 191: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
+ | {{dsc inc|cpp/utility/tuple/dsc operator{{=}}}} | ||
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 21:28, 25 October 2023
(1) | ||
pair& operator=( const pair& other ); |
(until C++20) | |
constexpr pair& operator=( const pair& other ); |
(since C++20) | |
constexpr const pair& operator=( const pair& other ) const; |
(2) | (since C++23) |
(3) | ||
template< class U1, class U2 > pair& operator=( const pair<U1, U2>& other ); |
(until C++20) | |
template< class U1, class U2 > constexpr pair& operator=( const pair<U1, U2>& other ); |
(since C++20) | |
template< class U1, class U2 > constexpr const pair& operator=( const pair<U1, U2>& other ) const; |
(4) | (since C++23) |
(5) | ||
pair& operator=( pair&& other ) noexcept(/* see below */); |
(since C++11) (until C++20) |
|
constexpr pair& operator=( pair&& other ) noexcept(/* see below */); |
(since C++20) | |
constexpr const pair& operator=( pair&& other ) const; |
(6) | (since C++23) |
(7) | ||
template< class U1, class U2 > pair& operator=( pair<U1, U2>&& p ); |
(since C++11) (until C++20) |
|
template< class U1, class U2 > constexpr pair& operator=( pair<U1, U2>&& p ); |
(since C++20) | |
template< class U1, class U2 > constexpr const pair& operator=( pair<U1, U2>&& p ) const; |
(8) | (since C++23) |
template< pair-like P > constexpr pair& operator=( P&& u ); |
(9) | (since C++23) |
template< pair-like P > constexpr const pair& operator=( P&& u ) const; |
(10) | (since C++23) |
Replaces the contents of the pair.
The assignment operator is implicitly declared. Using this assignment operator makes the program ill-formed if either |
(until C++11) |
This overload is defined as deleted if either std::is_copy_assignable<T1>::value or std::is_copy_assignable<T2>::value is false. |
(since C++11) |
first
and other.second to second
.
This overload participates in overload resolution only if std::is_assignable<T1&, const U1&>::value and std::is_assignable<T2&, const U2&>::value are both true. |
(since C++11) |
first
and other.second to second
.- std::same_as<std::remove_cvref_t<P>, std::pair> is false,
- std::remove_cvref_t<P> is not a specialization of std::ranges::subrange,
- std::is_assignable_v<T1&, decltype(std::get<0>(std::forward<P>(p)))> is true, and
- std::is_assignable_v<T1&, decltype(std::get<1>(std::forward<P>(p)))> is true.
- std::same_as<std::remove_cvref_t<P>, std::pair> is false,
- std::remove_cvref_t<P> is not a specialization of std::ranges::subrange,
- std::is_assignable_v<const T1&, decltype(std::get<0>(std::forward<P>(p)))> is true, and
- std::is_assignable_v<const T1&, decltype(std::get<1>(std::forward<P>(p)))> is true.
Contents |
[edit] Parameters
other | - | pair of values to replace the contents of this pair |
p | - | pair of values of possibly different types to replace the contents of this pair |
u | - | pair-like object of values to replace the contents of this pair |
Type requirements | ||
-T1 must meet the requirements of CopyAssignable from U1 . (until C++11)
| ||
-T2 must meet the requirements of CopyAssignable from U2 . (until C++11)
|
[edit] Return value
*this
[edit] Exceptions
std::is_nothrow_move_assignable<T1>::value &&
std::is_nothrow_move_assignable<T2>::value
[edit] Example
#include <cstddef> #include <iomanip> #include <iostream> #include <utility> #include <vector> template<class Os, class T> Os& operator<<(Os& os, const std::vector<T>& v) { os << '{'; for (std::size_t t = 0; t != v.size(); ++t) os << v[t] << (t + 1 < v.size() ? ", " : ""); return os << '}'; } template<class Os, class U1, class U2> Os& operator<<(Os& os, const std::pair<U1, U2>& pair) { return os << '{' << pair.first << ", " << pair.second << '}'; } int main() { std::pair<int, std::vector<int>> p{1, {2}}, q{2, {5, 6}}; p = q; // (1) operator=(const pair& other); std::cout << std::setw(23) << std::left << "(1) p = q;" << "p: " << p << " q: " << q << '\n'; std::pair<short, std::vector<int>> r{4, {7, 8, 9}}; p = r; // (3) operator=(const pair<U1, U2>& other); std::cout << std::setw(23) << "(3) p = r;" << "p: " << p << " r: " << r << '\n'; p = std::pair<int, std::vector<int>>{3, {4}}; p = std::move(q); // (5) operator=(pair&& other); std::cout << std::setw(23) << "(5) p = std::move(q);" << "p: " << p << " q: " << q << '\n'; p = std::pair<int, std::vector<int>>{5, {6}}; p = std::move(r); // (7) operator=(pair<U1, U2>&& other); std::cout << std::setw(23) << "(7) p = std::move(r);" << "p: " << p << " r: " << r << '\n'; }
Output:
(1) p = q; p: {2, {5, 6}} q: {2, {5, 6}} (3) p = r; p: {4, {7, 8, 9}} r: {4, {7, 8, 9}} (5) p = std::move(q); p: {2, {5, 6}} q: {2, {}} (7) p = std::move(r); p: {4, {7, 8, 9}} r: {4, {}}
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 885 | C++98 | missing heterogeneous copy assignment | added (as overload (3)) |
LWG 2729 | C++11 | pair::operator= was unconstrained and mightresult in unnecessary undefined behavior |
constrained |
[edit] See also
assigns the contents of one tuple to another (public member function of std::tuple<Types...> )
|