Namespaces
Variants

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

cppreference.net에서 가져옴
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐지)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
스왑 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
pair& operator=( const pair& other );
(1) (C++20부터 constexpr)
constexpr const pair& operator=( const pair& other ) const;
(2) (C++23부터)
template< class U1, class U2 >
pair& operator=( const pair<U1, U2>& other );
(3) (C++20부터 constexpr)
template< class U1, class U2 >
constexpr const pair& operator=( const pair<U1, U2>& other ) const;
(4) (C++23부터)
pair& operator=( pair&& other ) noexcept(/* see below */);
(5) (C++11부터)
(C++20부터 constexpr)
constexpr const pair& operator=( pair&& other ) const;
(6) (C++23부터)
template< class U1, class U2 >
pair& operator=( pair<U1, U2>&& p );
(7) (C++11부터)
(C++20부터 constexpr)
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부터)

쌍의 내용을 대체합니다.

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) std::forward<U1>(p.first)를 first에, std::forward<U2>(p.second)를 second에 대입합니다.
이 오버로드는 std::is_assignable_v<const T1&, U1>와 std::is_assignable_v<const T2&, U2>가 모두 true인 경우에만 오버로드 해결에 참여합니다.
9) std::get<0>(std::forward<P>(u))를 first에, std::get<1>(std::forward<P>(u))를 second에 대입합니다.
이 오버로드는 다음 조건이 모두 참일 때에만 오버로드 해결에 참여합니다:
  • std::same_as<std::remove_cvref_t<P>, std::pair>이 false이고,
  • std::remove_cvref_t<P>이 std::ranges::subrange의 특수화가 아니고,
  • std::is_assignable_v<T1&, decltype(std::get<0>(std::forward<P>(p)))>이 true이며, 그리고
  • std::is_assignable_v<T1&, decltype(std::get<1>(std::forward<P>(p)))>이 true입니다.
10) std::get<0>(std::forward<P>(u))를 first에, std::get<1>(std::forward<P>(u))를 second에 대입합니다.
이 오버로드는 다음 조건이 모두 참일 때에만 오버로드 해결에 참여합니다:
  • std::same_as<std::remove_cvref_t<P>, std::pair>이 false이고,
  • std::remove_cvref_t<P>이 std::ranges::subrange의 특수화가 아니고,
  • std::is_assignable_v<const T1&, decltype(std::get<0>(std::forward<P>(p)))>이 true이며, 그리고
  • std::is_assignable_v<const T1&, decltype(std::get<1>(std::forward<P>(p)))>이 true입니다.

매개변수

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 멤버 함수)