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の内容を置き換える値のペア |
| p | - | この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...>
の公開メンバ関数)
|