std::tuple<Types...>::operator=

来自cppreference.com
< cpp‎ | utility‎ | tuple
 
 
 
 
tuple& operator=(const tuple& other );
(1) (C++11 起)
(C++20 起为 constexpr)
constexprconst tuple& operator=(const tuple& other )const;
(2) (C++23 起)
tuple& operator=( tuple&& other )noexcept(/* 见下文 */);
(3) (C++11 起)
(C++20 起为 constexpr)
constexprconst 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>
constexprconst 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>
constexprconst tuple& operator=( tuple<UTypes...>&& other )const;
(8) (C++23 起)
template<class E1, class E2 >
tuple& operator=(conststd::pair<E1, E2>& p );
(9) (C++11 起)
(C++20 起为 constexpr)
template<class E1, class E2 >
constexprconst tuple& operator=(conststd::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 >
constexprconst 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 >
constexprconst tuple& operator=( UTuple&& u )const;
(14) (C++23 起)

以另一元组式对象的内容替换此元组的内容。

在以下描述中,令

  • i 在范围 [0sizeof...(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 的第二元素。
此重载只有在 时才会参与重载决议。
10)p.first 赋给首元素,并将 p.second 赋给第二元素。
此重载只有在 时才会参与重载决议。
11)std::forward<E1>(p.first) 赋给 *this 的首元素,并将 std::forward<E2>(p.second) 赋给 *this 的第二元素。
此重载只有在 时才会参与重载决议。
12)std::forward<E1>(p.first) 赋给首元素,并将 std::forward<E2>(p.second) 赋给第二元素。
此重载只有在 时才会参与重载决议。
13) 对所有 i,将 std::get<i>(std::forward<UTuple>(u)) 赋给 std::get<i>(*this)
此重载只有在 时才会参与重载决议。
14) 对所有 i,将 std::get<i>(std::forward<UTuple>(u)) 赋给 std::get<i>(*this)
此重载只有在 时才会参与重载决议。

目录

[编辑]参数

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, conststd::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, constchar*, 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<constchar*, 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> 的公开成员函数)[编辑]
close