std::pair<T1,T2>::operator=
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    | (1) | ||
|   pair& operator=( const pair& other );  | 
(C++20 前) | |
|   constexpr pair& operator=( const pair& other );  | 
(C++20 起) | |
|   constexpr const pair& operator=( const pair& other ) const;  | 
(2) | (C++23 起) | 
| (3) | ||
|   template< class U1, class U2 > pair& operator=( const pair<U1, U2>& other );  | 
(C++20 前) | |
|   template< class U1, class U2 > constexpr pair& operator=( const pair<U1, U2>& other );  | 
(C++20 起) | |
|   template< class U1, class U2 > constexpr const pair& operator=( const pair<U1, U2>& other ) const;  | 
(4) | (C++23 起) | 
| (5) | ||
|   pair& operator=( pair&& other ) noexcept(/* 见下文 */);  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr pair& operator=( pair&& other ) noexcept(/* 见下文 */);  | 
(C++20 起) | |
|   constexpr const pair& operator=( pair&& other ) const;  | 
(6) | (C++23 起) | 
| (7) | ||
|   template< class U1, class U2 > pair& operator=( pair<U1, U2>&& other );  | 
 (C++11 起)  (C++20 前)  | 
|
|   template< class U1, class U2 > constexpr pair& operator=( pair<U1, U2>&& other );  | 
(C++20 起) | |
|   template< class U1, class U2 > constexpr const pair& operator=( pair<U1, U2>&& other ) const;  | 
(8) | (C++23 起) | 
|   template< pair-like P > constexpr pair& operator=( P&& u );  | 
(9) | (C++23 起) | 
|   template< pair-like P > constexpr const pair& operator=( P&& u ) const;  | 
(10) | (C++23 起) | 
替换 pair 的内容。
1) 复制赋值运算符。以 other 内容的副本替换内容。
 
| 
 此赋值运算符被隐式声明。如果   | 
(C++11 前) | 
| 
 如果 std::is_copy_assignable<T1>::value 或 std::is_copy_assignable<T2>::value 之一是 false,那么定义此重载为被删除。  | 
(C++11 起) | 
2) const 限定操作数的复制赋值运算符。
3) 将 other.first 赋给 
first,other.second 赋给 second。
 | 
 此重载只有在 std::is_assignable<T1&, const U1&>::value 和 std::is_assignable<T2&, const U2&>::value 都是 true时才会参与重载决议。  | 
(C++11 起) | 
4) 将 other.first 赋给 
first,other.second 赋给 second。 此重载只有在std::is_assignable_v<const T1&, const U1&> 和 std::is_assignable_v<const T2&, const U2&> 都是 true时才会参与重载决议。
5) 移动赋值运算符。用移动语义以 other 的内容替换内容。
6) const 限定操作数的移动赋值运算符。
 此重载只有在
- std::same_as<std::remove_cvref_t<P>, std::pair> 是 false,
 - std::remove_cvref_t<P> 不是 std::ranges::subrange 的特化,
 - std::is_assignable_v<T1&, decltype(std::get<0>(std::forward<P>(p)))> 是 true,并且
 - std::is_assignable_v<T1&, decltype(std::get<1>(std::forward<P>(p)))> 是 true
 
 此重载只有在
- std::same_as<std::remove_cvref_t<P>, std::pair> 是 false,
 - std::remove_cvref_t<P> 不是 std::ranges::subrange 的特化,
 - std::is_assignable_v<const T1&, decltype(std::get<0>(std::forward<P>(p)))> 是 true,并且
 - std::is_assignable_v<const T1&, decltype(std::get<1>(std::forward<P>(p)))> 是 true
 
参数
| other | - |  包含用来替换此 pair 的值的 pair
 | 
| p | - |  包含用来替换此 pair 的(类型可能不同的)值的 pair
 | 
| u | - |  包含用来替换此 pair 的值的 pair-like 对象
 | 
| 类型要求 | ||
 -T1 必须符合从 U1 可复制赋值 (CopyAssignable)  的要求。(C++11 前)
 | ||
 -T2 必须符合从 U2 可复制赋值 (CopyAssignable)  的要求。(C++11 前)
 | ||
返回值
*this
异常
1-4) 可能会抛出由实现定义的异常。
5) 
noexcept 说明:  
noexcept(
    std::is_nothrow_move_assignable<T1>::value &&
    std::is_nothrow_move_assignable<T2>::value
6-10) 可能会抛出由实现定义的异常。
示例
运行此代码
#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'; }
输出:
(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, {}}
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 885 | C++98 | 缺失了异质复制赋值 | 已(作为重载 (3))添加 | 
| LWG 2729 | C++11 | operator= 未被约束并可能导致不必要的未定义行为 | 已约束 | 
参阅
   赋值一个 tuple 的内容给另一个 ( std::tuple<Types...> 的公开成员函数)  |