Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |utility‎ |tuple
       
       
      Utilities library
       
       
      tuple& operator=(const tuple& other);
      (1)(since C++11)
      (constexpr since C++20)
      constexprconst tuple& operator=(const tuple& other)const;
      (2)(since C++23)
      tuple& operator=( tuple&& other)noexcept(/* see below */);
      (3)(since C++11)
      (constexpr since C++20)
      constexprconst tuple& operator=( tuple&& other)const;
      (4)(since C++23)
      template<class...UTypes>
      tuple& operator=(const tuple<UTypes...>& other);
      (5)(since C++11)
      (constexpr since C++20)
      template<class...UTypes>
      constexprconst tuple& operator=(const tuple<UTypes...>& other)const;
      (6)(since C++23)
      template<class...UTypes>
      tuple& operator=( tuple<UTypes...>&& other);
      (7)(since C++11)
      (constexpr since C++20)
      template<class...UTypes>
      constexprconst tuple& operator=( tuple<UTypes...>&& other)const;
      (8)(since C++23)
      template<class E1,class E2>
      tuple& operator=(conststd::pair<E1, E2>& p);
      (9)(since C++11)
      (constexpr since C++20)
      template<class E1,class E2>
      constexprconst tuple& operator=(conststd::pair<E1, E2>& p)const;
      (10)(since C++23)
      template<class E1,class E2>
      tuple& operator=(std::pair<E1, E2>&& p);
      (11)(since C++11)
      (constexpr since C++20)
      template<class E1,class E2>
      constexprconst tuple& operator=(std::pair<E1, E2>&& p)const;
      (12)(since C++23)
      template< tuple-like UTuple>
      constexpr tuple& operator=( UTuple&& u);
      (13)(since C++23)
      template< tuple-like UTuple>
      constexprconst tuple& operator=( UTuple&& u)const;
      (14)(since C++23)

      Replaces the contents of the tuple with the contents of another tuple-like object.

      In the descriptions that follow, let

      • i be in the range[0sizeof...(Types)) in order,
      • Ti be theith type in the class template parameter packTypes, and
      • Ui be theith type in a function template parameter pack namedUTypes,

      where indexing is zero-based.

      1) Copy assignment operator. Assigns each element ofother to the corresponding element of*this.
      This overload is defined as deleted unlessstd::is_copy_assignable<Ti>::value istrue for allTi.
      2) Copy assignment operator for const-qualified operand. Assigns each element ofother to the corresponding element of*this.
      This overload participates in overload resolution only ifstd::is_copy_assignable_v<const Ti> istrue for allTi.
      3) Move assignment operator. For alli, assignsstd::forward<Ti>(std::get<i>(other)) tostd::get<i>(*this).
      This overload participates in overload resolution only ifstd::is_move_assignable<Ti>::value istrue for allTi.
      4) Move assignment operator for const-qualified operand. For alli, assignsstd::forward<Ti>(std::get<i>(other)) tostd::get<i>(*this).
      This overload participates in overload resolution only ifstd::is_assignable_v<const Ti&, Ti> istrue for allTi.
      5) For alli, assignsstd::get<i>(other) tostd::get<i>(*this).
      This overload participates in overload resolution only ifsizeof...(Types)== sizeof...(UTypes), andstd::is_assignable<Ti&,const Ui&>::value istrue for all corresponding pairs of typesTi andUi.
      6) For alli, assignsstd::get<i>(other) tostd::get<i>(*this).
      This overload participates in overload resolution only ifsizeof...(Types)== sizeof...(UTypes), andstd::is_assignable_v<const Ti&,const Ui&> istrue for all corresponding pairs of typesTi andUi.
      7) For alli, assignsstd::forward<Ui>(std::get<i>(other)) tostd::get<i>(*this).
      This overload participates in overload resolution only ifsizeof...(Types)== sizeof...(UTypes), andstd::is_assignable<Ti&, Ui>::value istrue for all corresponding pairs of typesTiandUi.
      8) For alli, assignsstd::forward<Ui>(std::get<i>(other)) tostd::get<i>(*this).
      This overload participates in overload resolution only ifsizeof...(Types)== sizeof...(UTypes), andstd::is_assignable_v<const Ti&, Ui> istrue for all corresponding pairs of typesTi andUi.
      9) Assignsp.first to the first element of*this andp.second to the second element of*this.
      This overload participates in overload resolution only if
      10) Assignsp.first to the first element andp.second to the second element.
      This overload participates in overload resolution only if
      11) Assignsstd::forward<E1>(p.first) to the first element of*this andstd::forward<E2>(p.second) to the second element of*this.
      This overload participates in overload resolution only if
      12) Assignsstd::forward<E1>(p.first) to the first element andstd::forward<E2>(p.second) to the second element.
      This overload participates in overload resolution only if
      13) For alli, assignsstd::get<i>(std::forward<UTuple>(u)) tostd::get<i>(*this).
      This overload participates in overload resolution only if
      14) For alli, assignsstd::get<i>(std::forward<UTuple>(u)) tostd::get<i>(*this).
      This overload participates in overload resolution only if

      Contents

      [edit]Parameters

      other - tuple to replace the contents of this tuple
      p - pair to replace the contents of this 2-tuple
      u -tuple-like object to replace the contents of this tuple

      [edit]Return value

      *this

      [edit]Exceptions

      1,2) May throw implementation-defined exceptionsif the assignment of one of the types inTypes throws an exception.
      3)
      noexcept specification:  
      noexcept(

         std::is_nothrow_move_assignable<T0>::value&&
         std::is_nothrow_move_assignable<T1>::value&&
         std::is_nothrow_move_assignable<T2>::value&&
          ...

      )
      4-14) May throw implementation-defined exceptionsif the assignment of one of the types inTypes throws an exception.

      [edit]Example

      Run this code
      #include <iostream>#include <string>#include <string_view>#include <tuple>#include <utility>#include <vector> // helper function to print 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<<'}';} // helpers to print a tuple of any sizetemplate<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(){// Tuple to tuple examples //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); // Normal copy assignment// operator=( const tuple& other );    t1= t2;    print_tuples("2) t1 = t2;\n   t1", t1,"t2", t2); // Normal move assignment// operator=( tuple&& other );    t1= std::move(t2);    print_tuples("3) t1 = std::move(t2);\n   t1", t1,"t2", t2); // Converting copy assignment// 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); // Converting move assignment// operator=( tuple<UTypes...>&& other );    t1= std::move(t3);    print_tuples("5) t1 = std::move(t3);\n   t1", t1,"t3", t3); // Pair to tuple examples //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); // Converting copy assignment from std::pair// operator=( const std::pair<U1, U2>& p );    t4= p1;    print_tuples("7) t4 = p1;\n   t4", t4,"p1", p1); // Converting move assignment from std::pair// operator=( std::pair<U1, U2>&& p );    t4= std::move(p1);    print_tuples("8) t4 = std::move(p1);\n   t4", t4,"p1", p1);}

      Possible output:

      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, {}}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2729C++11operator= was unconstrained and might
      result in unnecessary undefined behavior
      constrained

      [edit]See also

      constructs a newtuple
      (public member function)[edit]
      assigns the contents
      (public member function ofstd::pair<T1,T2>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/tuple/operator%3D&oldid=171724"

      [8]ページ先頭

      ©2009-2025 Movatter.jp