std::pair<T1,T2>::operator=
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부터) |
쌍의 내용을 대체합니다.
other의 내용의 복사본으로 대체합니다.
|
대입 연산자는 암시적으로 선언됩니다. 이 대입 연산자를 사용하면 |
(C++11까지) |
|
이 오버로드는 |
(C++11부터) |
std::is_copy_assignable_v<const T1>와 std::is_copy_assignable_v<const T2>가 모두 true인 경우에만 오버로드 해결에 참여합니다.other.first를 first에, other.second를 second에 대입합니다.
|
이 오버로드는 |
(C++11부터) |
other.first를 first에, other.second를 second에 대입합니다.std::is_assignable_v<const T1&, const U1&>와 std::is_assignable_v<const T2&, const U2&>가 모두 true인 경우에만 오버로드 해결에 참여합니다.other의 내용으로 대체합니다.std::is_move_assignable<T1>::value와 std::is_move_assignable<T2>::value가 모두 true인 경우에만 오버로드 해결에 참여합니다.std::is_assignable_v<const T1&, T1>와 std::is_assignable_v<const T2&, T2>가 모두 true인 경우에만 오버로드 해결에 참여합니다.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인 경우에만 오버로드 해결에 참여합니다.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인 경우에만 오버로드 해결에 참여합니다.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입니다.
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
예외
std::
is_nothrow_move_assignable
<
T1
>
::
value
&&
std::
is_nothrow_move_assignable
<
T2
>
::
value
예제
#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 멤버 함수)
|