std::pair<T1,T2>::operator=
From cppreference.com
(1) | ||
pair& operator=( const pair& other ); |
(until C++20) | |
constexpr pair& operator=( const pair& other ); |
(since C++20) | |
(2) | ||
template< class U1, class U2 > pair& operator=( const pair<U1,U2>& other ); |
(since C++11) (until C++20) |
|
template< class U1, class U2 > constexpr pair& operator=( const pair<U1,U2>& other ); |
(since C++20) | |
(3) | ||
pair& operator=( pair&& other ) noexcept(/* see below */); |
(since C++11) (until C++20) |
|
constexpr pair& operator=( pair&& other ) noexcept(/* see below */); |
(since C++20) | |
(4) | ||
template< class U1, class U2 > pair& operator=( pair<U1,U2>&& other ); |
(since C++11) (until C++20) |
|
template< class U1, class U2 > constexpr pair& operator=( pair<U1,U2>&& other ); |
(since C++20) | |
Replaces the contents of the pair.
1) Copy assignment operator. Replaces the contents with a copy of the contents of other.
|
(until C++11) |
|
(since C++11) |
2) Assigns
other.first
to first
and other.second
to second
.
- This overload participates in overload resolution only if std::is_assignable<first_type&, const U1&>::value and std::is_assignable<second_type&, const U2&>::value are both true.
3) Move assignment operator. Replaces the contents with those of
other
using move semantics.
- This overload participates in overload resolution only if std::is_move_assignable<first_type>::value and std::is_move_assignable<second_type>::value are both true.
4) Assigns std::forward<U1>(p.first) to
first
and std::forward<U2>(p.second) to second
.
- This overload participates in overload resolution only if std::is_assignable<first_type&, U1>::value and std::is_assignable<second_type&, U2>::value are both true.
Contents |
Parameters
other | - | pair of values to replace the contents of this pair |
Return value
*this
Exceptions
1-2) (none)
3)
noexcept specification:
noexcept(
std::is_nothrow_move_assignable<T1>::value &&
std::is_nothrow_move_assignable<T2>::value
4) (none)
Example
Run this code
#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(19) << std::left << "p = q;" << "p" << p << " q" << q << '\n'; std::pair<short, std::vector<int>> r{ 4, {7,8,9} }; p = r; // (2) operator=( const pair<U1,U2>& other ); std::cout << std::setw(19) << "p = r;" << "p" << p << " r" << r << '\n'; p = std::pair<int, std::vector<int>>{ 3, {4} }; p = std::move(q); // (3) operator=( pair&& other ); std::cout << std::setw(19) << "p = std::move(q);" << "p" << p << " q" << q << '\n'; p = std::pair<int, std::vector<int>>{ 5, {6} }; p = std::move(r); // (4) operator=( pair<U1,U2>&& other ); std::cout << std::setw(19) << "p = std::move(r);" << "p" << p << " r" << r << '\n'; }
Output:
(1) p = q; p:{ 2, {5,6} } q:{ 2, {5,6} } (2) p = r; p:{ 4, {7,8,9} } r:{ 4, {7,8,9} } (3) p = std::move(q); p:{ 2, {5,6} } q:{ 2, {} } (4) p = std::move(r); p:{ 4, {7,8,9} } r:{ 4, {} }
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 2729 | C++11 | pair::operator= was unconstrained and mightresult in unnecessary undefined behavior |
constrained |