std::pair<T1,T2>::operator=

Replaces the contents of the pair.

# Declarations

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;

(since C++23)

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;

(since C++23)

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;

(since C++23)

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;

(since C++23)

template< pair-like P >
constexpr pair& operator=( P&& u );

(since C++23)

template< pair-like P >
constexpr const pair& operator=( P&& u ) const;

(since C++23)

# Parameters

# Return value

*this

# 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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 885C++98missing heterogeneous copy assignmentadded (as overload (3))
LWG 2729C++11pair::operator= was unconstrained and mightresult in unnecessary undefined behaviorconstrained

# See also