std::tuple<Types...>::operator=
来自cppreference.com
| (1) | (C++11 起) (C++20 起为 constexpr) |
|
| |
(2) | (C++23 起) |
| (3) | (C++11 起) (C++20 起为 constexpr) |
|
| |
(4) | (C++23 起) |
| (5) | (C++11 起) (C++20 起为 constexpr) |
|
| |
(6) | (C++23 起) |
| (7) | (C++11 起) (C++20 起为 constexpr) |
|
| |
(8) | (C++23 起) |
| (9) | (C++11 起) (C++20 起为 constexpr) |
|
| |
(10) | (C++23 起) |
| (11) | (C++11 起) (C++20 起为 constexpr) |
|
| |
(12) | (C++23 起) |
| |
(13) | (C++23 起) |
| |
(14) | (C++23 起) |
以另一元组式对象的内容替换此元组的内容。
在以下描述中,令
i在范围[0,sizeof...(Types))中依序取值,Ti为类模板形参包Types中的第i个类型,以及Ui为名字是UTypes的函数模板形参包中的第i个类型,
其中索引基于零。
1) 复制赋值运算符。赋值
other 的每个元素给 *this 的对应元素。 定义此重载为弃置,除非
std::is_copy_assignable<T_i>::value 对所有 Ti 都是 true。2) 针对 const 限定操作数的复制赋值运算符。赋值
other 的每个元素给 *this 的对应元素。 此重载只有在
std::is_copy_assignable_v<const Ti> 对所有 Ti 都是 true 时才会参与重载决议。3) 移动赋值运算符。对所有
i,将 std::forward<Ti>(std::get<i>(other)) 赋给 std::get<i>(*this)。 此重载只有在
std::is_move_assignable<Ti>::value 对所有 Ti 都是 true 时才会参与重载决议。4) 针对 const 限定操作数的移动赋值运算符。对所有
i,将 std::forward<Ti>(std::get<i>(other)) 赋给 std::get<i>(*this)。 此重载只有在
std::is_assignable_v<const Ti&, Ti> 对所有 Ti 都是 true 时才会参与重载决议。5) 对所有
i,将 std::get<i>(other) 赋给 std::get<i>(*this)。 此重载只有在
sizeof...(Types) == sizeof...(UTypes),并且 std::is_assignable<Ti&, const Ui&>::value 对所有 Ti 和对应的 Ui 都是 true 时才会参与重载决议。6) 对所有
i,将 std::get<i>(other) 赋给 std::get<i>(*this)。 此重载只有在
sizeof...(Types) == sizeof...(UTypes),并且 std::is_assignable_v<const Ti&, const Ui&> 对所有 Ti 和对应的 Ui 都是 true 时才会参与重载决议。7) 对所有
i,将 std::forward<Ui>(std::get<i>(other)) 赋给 std::get<i>(*this)。 此重载只有在
sizeof...(Types) == sizeof...(UTypes),并且 std::is_assignable<Ti&, Ui>::value 对所有 Ti 和对应的 Ui 都是 true 时才会参与重载决议。8) 对所有
i,将 std::forward<Ui>(std::get<i>(other)) 赋给 std::get<i>(*this)。 此重载只有在
sizeof...(Types) == sizeof...(UTypes),并且 std::is_assignable_v<const Ti&, Ui> 对所有 Ti 和对应的 Ui 都是 true 时才会参与重载决议。9) 将
p.first 赋给 *this 的首元素,并将 p.second 赋给 *this 的第二元素。 此重载只有在
sizeof...(Types) == 2,std::is_assignable<T0&, const E1&>::value是true,并且std::is_assignable<T1&, const E2&>::value是true
10) 将
p.first 赋给首元素,并将 p.second 赋给第二元素。 此重载只有在
sizeof...(Types) == 2,std::is_assignable_v<const T0&, const E1&>是true,并且std::is_assignable_v<const T1&, const E2&>是true
11) 将
std::forward<E1>(p.first) 赋给 *this 的首元素,并将 std::forward<E2>(p.second) 赋给 *this 的第二元素。 此重载只有在
sizeof...(Types) == 2,std::is_assignable_v<T0&, E1>是true,并且std::is_assignable_v<T1&, E2>是true
12) 将
std::forward<E1>(p.first) 赋给首元素,并将 std::forward<E2>(p.second) 赋给第二元素。 此重载只有在
sizeof...(Types) == 2,std::is_assignable_v<const T0&, E1>是true,并且std::is_assignable_v<const T1&, E2>是true
13) 对所有
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)))>对所有i都是true
14) 对所有
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)))>对所有i都是true
参数
| other | - | 要替换此元组内容的元组 |
| p | - | 要替换此 2-元组内容的对偶 |
| u | - | 要替换此元组内容的 tuple-like 对象
|
返回值
*this
异常
1,2) 在
Types 中的类型赋值抛出异常时可能会抛出由实现定义的异常。3)
noexcept 说明:
noexcept( std::is_nothrow_move_assignable<T0>::value && std::is_nothrow_move_assignable<T1>::value && std::is_nothrow_move_assignable<T2>::value && ... )4-14) 在
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";
}
int main()
{
// 元组向元组赋值的示例 //
std::tuple<int, std::string, std::vector<int>>
t1{1, "alpha", {1, 2, 3}},
t2{2, "beta", {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, "gamma", {6, 7, 8}};
t1 = t3;
print_tuples("4) t1 = t3;\n t1", t1, "t3", t3);
// 转换移动赋值
// operator=( tuple<UTypes...>&& 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{"epsilon", {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++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2729 | C++11 | operator= 未被约束并可能导致不必要的未定义行为
|
已约束 |
参阅
构造新的 tuple (公开成员函数) | |
| 赋值内容 ( std::pair<T1,T2> 的公开成员函数)
|