Namespaces
Variants

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

From cppreference.net
Utilities library
(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 ( /* see below */ ) ;
(C++11 이후)
(C++20 이전)
constexpr pair & operator = ( pair && other ) noexcept ( /* see below */ ) ;
(C++20 이후)
constexpr const pair & operator = ( pair && other ) const ;
(6) (C++23 이후)
(7)
template < class U1, class U2 >
pair & operator = ( pair < U1, U2 > && p ) ;
(C++11부터)
(C++20까지)
template < class U1, class U2 >
constexpr pair & operator = ( pair < U1, U2 > && p ) ;
(C++20부터)
template < class U1, class U2 >
constexpr const pair & operator = ( pair < U1, U2 > && p ) 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 의 내용 복사본으로 대체합니다.

할당 연산자는 암시적으로 선언됩니다. 이 할당 연산자를 사용하면 T1 또는 T2 가 const 한정 타입, 참조 타입, 접근 불가능한 복사 할당 연산자를 가진 클래스 타입, 또는 그러한 클래스의 배열 타입일 경우 프로그램이 형식 오류를 일으킵니다.

(C++11 이전)

이 오버로드는 std:: is_copy_assignable < T1 > :: value 또는 std:: is_copy_assignable < T2 > :: value false 인 경우 삭제된 것으로 정의됩니다.

(C++11 이후)
2) const 한정 피연산자에 대한 복사 할당 연산자.
이 오버로드는 다음 조건이 모두 std:: is_copy_assignable_v < const T1 > std:: is_copy_assignable_v < const T2 > 가 모두 true 인 경우에만 오버로드 해결에 참여합니다.
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 의 내용으로 대체합니다.
이 오버로드는 다음 조건이 모두 std:: is_move_assignable < T1 > :: value std:: is_move_assignable < T2 > :: value 가 모두 true 인 경우에만 오버로드 해결에 참여합니다.
6) const 한정 피연산자에 대한 이동 할당 연산자.
이 오버로드는 다음 조건이 모두 std:: is_assignable_v < const T1 & , T1 > std:: is_assignable_v < const T2 & , T2 > 가 모두 true 일 때만 오버로드 해결에 참여합니다.
7) std:: forward < U1 > ( p. first ) first 에 할당하고, std:: forward < U2 > ( p. second ) second 에 할당합니다.
이 오버로드는 다음 조건이 모두 std:: is_assignable < T1 & , U1 > :: value std:: is_assignable < T2 & , U2 > :: value 가 모두 true 인 경우에만 오버로드 해결에 참여합니다.
8) first std:: forward < U1 > ( p. first ) 를 할당하고, second std:: forward < U2 > ( p. second ) 를 할당합니다.
이 오버로드는 다음 조건이 모두 std:: is_assignable_v < const T1 & , U1 > std:: is_assignable_v < const T2 & , U2 > 가 모두 true 일 때만 오버로드 해결에 참여합니다.
9) first std :: get < 0 > ( std:: forward < P > ( u ) ) 를 할당하고, second std :: get < 1 > ( std:: forward < P > ( u ) ) 를 할당합니다.
이 오버로드는 다음 조건을 모두 만족할 때만 오버로드 해결에 참여합니다:
10) first std :: get < 0 > ( std:: forward < P > ( u ) ) 를 할당하고, second std :: get < 1 > ( std:: forward < P > ( u ) ) 를 할당합니다.
이 오버로드는 다음 조건을 모두 만족할 때만 오버로드 해결에 참여합니다:

목차

매개변수

other - 이 pair의 내용을 대체할 값들의 pair
p - 이 pair의 내용을 대체할 가능한 다른 타입의 값들로 구성된 pair
u - pair-like 이 pair의 내용을 대체할 값들의 객체
타입 요구사항
-
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 <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';
}

출력:

(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++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 885 C++98 이종 복사 할당 연산자 누락 추가됨 (오버로드 (3) 으로)
LWG 2729 C++11 pair::operator= 가 제약되지 않아 불필요한
미정의 동작이 발생할 수 있었음
제약 조건 추가

참고 항목

하나의 tuple 내용을 다른 tuple 에 할당
( std::tuple<Types...> 의 public 멤버 함수)