std::tuple<Types...>::operator=
tuple& operator=( const tuple& other );
|
(1) | (C++11 から) (C++20 から constexpr) |
constexpr const tuple& operator=( const tuple& other ) const;
|
(2) | (C++23 から) |
tuple& operator=( tuple&& other ) noexcept(/* see below */);
|
(3) | (C++11 から) (C++20 から constexpr) |
constexpr const tuple& operator=( tuple&& other ) const;
|
(4) | (C++23 から) |
template< class... UTypes >
tuple& operator=( const tuple<UTypes...>& other );
|
(5) | (C++11 から) (C++20 から constexpr) |
template< class... UTypes >
constexpr const tuple& operator=( const tuple<UTypes...>& other ) const;
|
(6) | (C++23 から) |
template< class... UTypes >
tuple& operator=( tuple<UTypes...>&& other );
|
(7) | (C++11 から) (C++20 から constexpr) |
template< class... UTypes >
constexpr const tuple& operator=( tuple<UTypes...>&& other ) const;
|
(8) | (C++23 から) |
template< class E1, class E2 >
tuple& operator=( const std::pair<E1, E2>& p );
|
(9) | (C++11 から) (C++20 から constexpr) |
template< class E1, class E2 >
constexpr const tuple& operator=( const std::pair<E1, E2>& p ) const;
|
(10) | (C++23 から) |
template< class E1, class E2 >
tuple& operator=( std::pair<E1, E2>&& p );
|
(11) | (C++11 から) (C++20 から constexpr) |
template< class E1, class E2 >
constexpr const tuple& operator=( std::pair<E1, E2>&& p ) const;
|
(12) | (C++23 から) |
template< tuple-like UTuple >
constexpr tuple& operator=( UTuple&& u );
|
(13) | (C++23 から) |
template< tuple-like UTuple >
constexpr const tuple& operator=( UTuple&& u ) const;
|
(14) | (C++23 から) |
tuple の内容を、別の tuple のようなオブジェクトの内容で置き換えます。
以下の説明では、次のように表記します。
iは範囲[0,sizeof...(Types))内の値を順に取り、Tiはクラステンプレートの型パラメータパックiのTypes番目の型であり、Uiは関数テンプレートの型パラメータパックiのUTypes番目の型です。
ここで、インデックスは 0 から始まります。
other の各要素を *this の対応する要素に代入します。std::is_copy_assignable<Ti>::value について true が成立しない限り、このオーバーロードは deleted として定義されます。Ti2)other の対応する要素に代入します。*this すべての std::is_copy_assignable_v<const Ti> が成立する場合にのみ、このオーバーロードはオーバーロード解決に参加します。true3)Ti ムーブ代入演算子。すべての i に代入します。std::forward<Ti>(std::get<i>(other)) すべての std::get<i>(*this) について std::is_move_assignable<Ti>::value4)true const 修飾されたオペランドに対するムーブ代入演算子。すべての Ti について、i すべての std::forward<Ti>(std::get<i>(other)) について std::get<i>(*this) が成立する場合にのみ、このオーバーロードはオーバーロード解決に参加します。std::is_assignable_v<const Ti&, Ti> すべての true について、Ti を i かつ std::get<i>(other) が対応するすべての型の組 std::get<i>(*this) と sizeof...(Types) == sizeof...(UTypes) である場合にのみ、オーバーロード解決に参加します。std::is_assignable<Ti&, const Ui&>::value6)true すべての Ti について、Ui を i かつ std::get<i>(other) が対応するすべての型の組 std::get<i>(*this) と sizeof...(Types) == sizeof...(UTypes) である場合にのみ、オーバーロード解決に参加します。std::is_assignable_v<const Ti&, const Ui&>7)true すべての Ti について、Ui を i かつ std::forward<Ui>(std::get<i>(other)) が対応するすべての型の組 std::get<i>(*this) と sizeof...(Types) == sizeof...(UTypes) である場合にのみ、オーバーロード解決に参加します。std::is_assignable<Ti&, Ui>::value8)true すべての Ti について、Ui を i かつ std::forward<Ui>(std::get<i>(other)) が対応するすべての型の組 std::get<i>(*this) と sizeof...(Types) == sizeof...(UTypes) である場合にのみ、オーバーロード解決に参加します。std::is_assignable_v<const Ti&, Ui>9)true Ti を Ui の最初の要素に、p.first このオーバーロードは、
*thisp.second が *this であり、かつsizeof...(Types) == 2がstd::is_assignable<T0&, const E1&>::valueである場合にのみ、オーバーロード解決に参加します。true10)std::is_assignable<T1&, const E2&>::valuetrueを最初の要素に、
p.firstp.second が sizeof...(Types) == 2std::is_assignable_v<const T0&, const E1&>がtrueである場合にのみ、オーバーロード解決に参加します。std::is_assignable_v<const T1&, const E2&>11)true
std::forward<E1>(p.first) を *this の 2 番目の要素に代入します。std::forward<E2>(p.second) このオーバーロードは、
*thissizeof...(Types) == 2であり、かつstd::is_assignable_v<T0&, E1>trueがstd::is_assignable_v<T1&, E2>である場合にのみ、オーバーロード解決に参加します。true12)
std::forward<E1>(p.first) を 2 番目の要素に代入します。std::forward<E2>(p.second) このオーバーロードは、
sizeof...(Types) == 2がstd::is_assignable_v<const T0&, E1>であり、かつtruestd::is_assignable_v<const T1&, E2>がtrueである場合にのみ、オーバーロード解決に参加します。
i について、std::get<i>(std::forward<UTuple>(u)) を std::get<i>(*this) に代入します。std::same_as<std::remove_cvref_t<UTuple>, std::tuple>falseがstd::remove_cvref_t<UTuple>であり、std::ranges::subrange の特殊化ではなく、sizeof...(Types)std::tuple_size_v<std::remove_cvref_t<UTuple>>に等しく、かつstd::is_assignable_v<Ti&, decltype(std::get<i>(std::forward<UTuple>(u)))>がすべてのtrueについてiである場合にのみ、オーバーロード解決に参加します。
i について、std::get<i>(std::forward<UTuple>(u)) を std::get<i>(*this) に代入します。std::same_as<std::remove_cvref_t<UTuple>, std::tuple>falseがstd::remove_cvref_t<UTuple>であり、std::ranges::subrange の特殊化ではなく、sizeof...(Types)std::tuple_size_v<std::remove_cvref_t<UTuple>>に等しく、かつstd::is_assignable_v<const Ti&, decltype(std::get<i>(std::forward<UTuple>(u)))>がすべてのtrueについてiである場合にのみ、オーバーロード解決に参加します。
パラメータ
| other | - | このタプルの内容を置き換えるタプル |
| p | - | この2タプルの内容を置き換えるペア |
| u | - |
tuple-like
このタプルの内容を置き換えるタプルライクオブジェクト
|
戻り値
* this
例外
Types内のいずれかの型の代入が例外をスローする場合、実装定義の例外をスローする可能性があります。
std::
is_nothrow_move_assignable
<
T0
>
::
value
&&
std::
is_nothrow_move_assignable
<
T1
>
::
value
&&
std::
is_nothrow_move_assignable
<
T2
>
::
value
&&
...
Types内のいずれかの型の代入が例外をスローする場合、実装定義の例外をスローする可能性があります。
例
#include <iostream> #include <string> #include <string_view> #include <tuple> #include <utility> #include <vector> // std::vector<int> を出力するヘルパー関数 std::ostream& operator<<(std::ostream& os, std::vector<int> const& v) { os << '{'; for (std::size_t t = 0; t != v.size(); ++t) os << v[t] << (t + 1 < v.size() ? ", " : ""); return os << '}'; } // 任意サイズのタプルを出力するためのヘルパー template<class... Args> void print_tuple(std::string_view name, const std::tuple<Args...>& t) { std::cout << name << " = {"; std::apply([&](auto&& arg, auto&&... args) { std::cout << arg; ((std::cout << ", " << args), ...); }, t); std::cout << '}'; } template<class Tuple1, class Tuple2> void print_tuples(std::string_view name1, const Tuple1& t1, std::string_view name2, const Tuple2& t2) { print_tuple(name1, t1); std::cout << ", "; print_tuple(name2, std::tuple(t2)); std::cout << "\n\n" (注:指定された条件により、HTMLタグ・属性は保持され、タグ内のC++文字列エスケープシーケンス「\n」は翻訳対象外のため、原文のまま維持されています); } int main() { // タプルからタプルへの変換例 // std::tuple<int, std::string, std::vector<int>> t1{1, "alpha", {1, 2, 3}}, t2{2, "ベータ", {4, 5}}; print_tuples("1) t1", t1, "t2", t2); // 通常のコピー代入 // operator=( const tuple& other ); t1 = t2; print_tuples("2) t1 = t2;\n t1", t1, "t2", t2); // 通常のムーブ代入 // operator=( tuple&& other ); t1 = std::move(t2); print_tuples("3) t1 = std::move(t2);\n t1", t1, "t2", t2); // コピー代入の変換 // operator=( const tuple<UTypes...>& other ); std::tuple<short, const char*, std::vector<int>> t3{3, "ガンマ", {6, 7, 8}}; t1 = t3; print_tuples("4) t1 = t3;\n t1", t1, "t3", t3); // ムーブ代入の変換 // operator=( tuple&& other ); t1 = std::move(t3); print_tuples("5) t1 = std::move(t3);\n t1", t1, "t3", t3); // ペアからタプルへの変換例 // std::tuple<std::string, std::vector<int>> t4{"delta", {10, 11, 12}}; std::pair<const char*, std::vector<int>> p1{"イプシロン", {14, 15, 16}}; print_tuples("6) t4", t4, "p1", p1); // std::pairからのコピー代入の変換 // operator=( const std::pair<U1, U2>& p ); t4 = p1; print_tuples("7) t4 = p1;\n t4", t4, "p1", p1); // std::pairからのムーブ代入の変換 // operator=( std::pair<U1, U2>&& p ); t4 = std::move(p1); print_tuples("8) t4 = std::move(p1);\n t4", t4, "p1", p1); }
出力例:
1) t1 = {1, alpha, {1, 2, 3}}, t2 = {2, beta, {4, 5}}
2) t1 = t2;
t1 = {2, beta, {4, 5}}, t2 = {2, beta, {4, 5}}
3) t1 = std::move(t2);
t1 = {2, beta, {4, 5}}, t2 = {2, , {}}
4) t1 = t3;
t1 = {3, gamma, {6, 7, 8}}, t3 = {3, gamma, {6, 7, 8}}
5) t1 = std::move(t3);
t1 = {3, gamma, {6, 7, 8}}, t3 = {3, gamma, {}}
6) t4 = {delta, {10, 11, 12}}, p1 = {epsilon, {14, 15, 16}}
7) t4 = p1;
t4 = {epsilon, {14, 15, 16}}, p1 = {epsilon, {14, 15, 16}}
8) t4 = std::move(p1);
t4 = {epsilon, {14, 15, 16}}, p1 = {epsilon, {}}
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2729 | C++11 |
operator
=
は制約がなく、不要な未定義動作を
引き起こす可能性があった |
制約を追加 |
関連項目
新しい
tuple
を構築する
(public member function) |
|
|
内容を代入する
(public member function of
std::pair<T1,T2>
)
|