std::pair::operator=(3)
π NAME π
okay so we're in the C++ Standard Libary manual, page std::pair::operator=(3), and the NAME line is literally std::pair::operator= - std::pair::operator=. iconic, no notes π
β¨ Synopsis β¨
bestie, there are ten whole overloads. ten. hydrate before reading:
pair& operator=( const pair& other ); (1) (until C++20)
constexpr pair& operator=( const pair& other ); (1) (since C++20)
constexpr const pair& operator=( const pair& other ) const; (2) (since C++23)
template< class U1, class U2 >
pair& operator=( const pair<U1, U2>& other ); (3) (until C++20)
template< class U1, class U2 >
constexpr pair& operator=( const pair<U1, U2>& other ); (3) (since C++20)
template< class U1, class U2 >
constexpr const pair& operator=( const pair<U1, U2>& other ) const; (4) (since C++23)
pair& operator=( pair&& other ) noexcept(/* see below */); (5) (since C++11) (until C++20)
constexpr pair& operator=( pair&& other ) noexcept(/* see below */); (5) (since C++20)
constexpr const pair& operator=( pair&& other ) const; (6) (since C++23)
template< class U1, class U2 >
pair& operator=( pair<U1, U2>&& p ); (7) (since C++11) (until C++20)
template< class U1, class U2 >
constexpr pair& operator=( pair<U1, U2>&& p ); (7) (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. that's the whole vibe of this page, fr fr.
- Copy assignment operator. Replaces the contents with a copy of the contents of
other.
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. (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)
- Copy assignment operator for const-qualified operand.
This overload participates in overload resolution only if std::is_copy_assignable_v<const T1> and std::is_copy_assignable_v<const T2> are both true.
- Assigns
other.firsttofirstandother.secondtosecond.
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)
- Assigns
other.firsttofirstandother.secondtosecond.
This overload participates in overload resolution only if std::is_assignable_v<const T1&, const U1&> and std::is_assignable_v<const T2&, const U2&> are both true.
- Move assignment operator. Replaces the contents with those of
otherusing move semantics.
This overload participates in overload resolution only if std::is_move_assignable<T1>::value and std::is_move_assignable<T2>::value are both true.
- Move assignment operator for const-qualified operand.
This overload participates in overload resolution only if std::is_assignable_v<const T1&, T1> and std::is_assignable_v<const T2&, T2> are both true.
- Assigns
std::forward<U1>(p.first)tofirstandstd::forward<U2>(p.second)tosecond.
This overload participates in overload resolution only if std::is_assignable<T1&, U1>::value and std::is_assignable<T2&, U2>::value are both true.
- Assigns
std::forward<U1>(p.first)tofirstandstd::forward<U2>(p.second)tosecond.
This overload participates in overload resolution only if std::is_assignable_v<const T1&, U1> and std::is_assignable_v<const T2&, U2> are both true.
- Assigns
std::get<0>(std::forward<P>(u))tofirstandstd::get<1>(std::forward<P>(u))tosecond.
This overload participates in overload resolution only if
std::same_as<std::remove_cvref_t<P>, std::pair>isfalse,std::remove_cvref_t<P>is not a specialization ofstd::ranges::subrange,std::is_assignable_v<T1&, decltype(std::get<0>(std::forward<P>(p)))>istrue, andstd::is_assignable_v<T1&, decltype(std::get<1>(std::forward<P>(p)))>istrue.
- Assigns
std::get<0>(std::forward<P>(u))tofirstandstd::get<1>(std::forward<P>(u))tosecond.
This overload participates in overload resolution only if
std::same_as<std::remove_cvref_t<P>, std::pair>isfalse,std::remove_cvref_t<P>is not a specialization ofstd::ranges::subrange,std::is_assignable_v<const T1&, decltype(std::get<0>(std::forward<P>(p)))>istrue, andstd::is_assignable_v<const T1&, decltype(std::get<1>(std::forward<P>(p)))>istrue.
π Parameters π
otherβ pair of values to replace the contents of this pairpβ pair of values of possibly different types to replace the contents of this pairuβ pair-like object of values to replace the contents of this pair
π¦ Type requirements π¦
T1must meet the requirements ofCopyAssignablefromU1.(until C++11)T2must meet the requirements ofCopyAssignablefromU2.(until C++11)
πΈ Return value πΈ
*this
π₯ Exceptions π₯
- 1-4) May throw implementation-defined exceptions.
- 5)
noexceptspecification:
noexcept(
std::is_nothrow_move_assignable<T1>::value &&
std::is_nothrow_move_assignable<T2>::value
)
- 6-10) May throw implementation-defined exceptions.
π Example π
// Run this code
#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, {}}
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 885β applied toC++98β behavior as published: missing heterogeneous copy assignment; correct behavior: added (as overload (3)).LWG 2729β applied toC++11β behavior as published:pair::operator=was unconstrained and might result in unnecessary undefined behavior; correct behavior: constrained.
π See also π
operator=β assigns the contents of one tuple to another (public member function ofstd::tuple<Types...>)
π·οΈ Category π·οΈ
- conditionally
noexcept
made with π from http://cppreference.com, 2024.06.10, page std::pair::operator=(3) β that's the footer bestie, we're done π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms