std::pair<T1,T2>::operator=
pair& operator=( const pair<U1, U2>& other );
constexpr pair& operator=( const pair<U1, U2>& other );
constexpr const pair& operator=( const pair<U1, U2>& other ) const;
(until C++20)
pair& operator=( pair<U1, U2>&& p );
(until C++20)
constexpr pair& operator=( pair<U1, U2>&& p );
constexpr const pair& operator=( pair<U1, U2>&& p ) const;
constexpr pair& operator=( P&& u );
constexpr const pair& operator=( P&& u ) const;
Replaces the contents of the pair.
The assignment operator is implicitly declared. Using this assignment operator makes the program ill-formed if either T1
or 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.
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
.first
and std::get<1>(std::forward <P>(u)) 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.
first
and std::get<1>(std::forward <P>(u)) 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 <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.
[edit] Parameters
[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 |