Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | utility‎ | tuple
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
std::tuple
Member functions
tuple::operator=
Non-member functions
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Deduction guides(C++17)
Helper classes
 
(1)
tuple& operator=( const tuple& other );
(since C++11)
(until C++20)
constexpr tuple& operator=( const tuple& other );
(since C++20)
(2)
tuple& operator=( tuple&& other ) noexcept(/* see below */);
(since C++11)
(until C++20)
constexpr tuple& operator=( tuple&& other ) noexcept(/* see below */);
(since C++20)
(3)
template< class... UTypes >
tuple& operator=( const tuple<UTypes...>& other );
(since C++11)
(until C++20)
template< class... UTypes >
constexpr tuple& operator=( const tuple<UTypes...>& other );
(since C++20)
(4)
template< class... UTypes >
tuple& operator=( tuple<UTypes...>&& other );
(since C++11)
(until C++20)
template< class... UTypes >
constexpr tuple& operator=( tuple<UTypes...>&& other );
(since C++20)
(5)
template< class U1, class U2 >
tuple& operator=( const std::pair<U1,U2>& p );
(since C++11)
(until C++20)
template< class U1, class U2 >
constexpr tuple& operator=( const std::pair<U1,U2>& p );
(since C++20)
(6)
template< class U1, class U2 >
tuple& operator=( std::pair<U1,U2>&& p );
(since C++11)
(until C++20)
template< class U1, class U2 >
constexpr tuple& operator=( std::pair<U1,U2>&& p );
(since C++20)

Replaces the contents of the tuple with the contents of another tuple or a pair.

1) Copy assignment operator. Assigns each element of other to the corresponding element of *this.
2) Move assignment operator. For all i, assigns std::forward<Ti>(get<i>(other)) to get<i>(*this).
  • This overload only participates in overload resolution if std::is_move_assignable<T_i>::value is true for all T_i in Types.
3) For all i, assigns std::get<i>(other) to std::get<i>(*this).
  • This overload only participates in overload resolution if sizeof...(UTypes) == sizeof...(Types) and std::is_assignable<T_i&, const U_i&>::value is true for all corresponding pairs of types T_i in Types and U_i in UTypes.
4) For all i, assigns std::forward<Ui>(std::get<i>(other)) to std::get<i>(*this).
  • This overload only participates in overload resolution if sizeof...(UTypes) == sizeof...(Types) and std::is_assignable<T_i&, U_i>::value is true for all corresponding pairs of types T_i in Types and U_i in UTypes.
5) Assigns p.first to the first element of *this and p.second to the second element of *this.
  • This overload only participates in overload resolution if sizeof...(Types) == 2, std::is_assignable<T_0&, const U1&>::value and std::is_assignable<T_1&, const U2&>::value are both true, where T_0 and T_1 are the two types constituting Types.
6) Assigns std::forward<U1>(p.first) to the first element of *this and std::forward<U2>(p.second) to the second element of *this.
  • This overload only participates in overload resolution if std::is_assignable<T_0&, U1>::value and std::is_assignable<T_1&, U2>::value are both true, where T_0 and T_1 are the two types constituting Types.

Contents

[edit] Parameters

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

[edit] Return value

*this

[edit] Exceptions

1) (none)
2)
noexcept specification:  
noexcept(

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

)
3-6) (none)

[edit] Example

#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
 
// helper function to print std::vector
template<class Os, class T> Os& operator<< (Os& os, std::vector<T> 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 size
template<class Os, class Tuple, std::size_t N>
struct TuplePrinter {
    static void print(Os& os, const Tuple& t) {
        TuplePrinter<Os, Tuple, N-1>::print(os, t);
        os << ", " << std::get<N-1>(t);
    }
};
 
template<class Os, class Tuple>
struct TuplePrinter<Os, Tuple, 1>{
    static void print(Os& os, const Tuple& t) {
        os << std::get<0>(t);
    }
};
 
template<class Os, class... Args>
Os& operator<< (Os& os, const std::tuple<Args...>& t) {
    os << "{ ";
    TuplePrinter<Os, decltype(t), sizeof...(Args)>::print(os, t);
    return os << " }";
}
 
struct line{ int len{60}; };
template<class Os> Os& operator<< (Os& os, line l) {
    while (l.len-- > 0) std::cout << "─";
    return os << '\n';
}
 
int main() {
    std::tuple<int, std::string, std::vector<int>>
        t1{1, "alpha", {1, 2, 3} }, t2{2, "beta", {4, 5} };
    std::cout << "t1 = " << t1 << ", t2 = " << t2 << '\n';
    t1 = t2; // (1) operator=( const tuple& other );
    std::cout << "t1 = t2;\n" "t1 = " << t1 << ", t2 = " << t2 << '\n' << line{};
 
    t1 = std::move(t2); // (2) operator=( tuple&& other );
    std::cout << "t1 = std::move(t2);\n" "t1 = " << t1 << ", t2 = " << t2 << '\n' << line{};
 
    std::tuple<short, const char*, std::vector<int>> t3{3, "gamma", {6,7,8} };
    t1 = t3; // (3) operator=( const tuple<UTypes...>& other );
    std::cout << "t1 = t3; \n" "t1 = " << t1 << ", t3 = " << t3 << '\n' << line{};
 
    t1 = std::move(t3); // (4) operator=( tuple<UTypes...>&& other );
    std::cout << "t1 = std::move(t3);\n" "t1 = " << t1 << ", t3 = " << t3 << '\n' << line{};
 
    std::tuple<std::string, std::vector<int>> t4{"delta", {10,11,12} };
    std::pair<const char*, std::vector<int>> p1{"epsilon", {14,15,16} };
    std::cout << "t4 = " << t4 << ", "
              << "p1 = { " << p1.first << ", " << p1.second << " };\n";
    t4 = p1; // (5) operator=( const std::pair<U1,U2>& p );
    std::cout << "t4 = p1;\n" "t4 = " << t4
              << ", p1 = { " << p1.first << ", " << p1.second << " };\n" << line{};
 
    t4 = std::move(p1); // (6) operator=( std::pair<U1,U2>&& p );
    std::cout << "t4 = std::move(p1);\n" "t4 = " << t4
              << ", p1 = { " << p1.first << ", " << p1.second << " };\n";
}

Output:

t1 = { 1, alpha, {1,2,3} }, t2 = { 2, beta, {4,5} }
t1 = t2;
t1 = { 2, beta, {4,5} }, t2 = { 2, beta, {4,5} }
────────────────────────────────────────────────────────────
t1 = std::move(t2);
t1 = { 2, beta, {4,5} }, t2 = { 2, , {} }
────────────────────────────────────────────────────────────
t1 = t3;
t1 = { 3, gamma, {6,7,8} }, t3 = { 3, gamma, {6,7,8} }
────────────────────────────────────────────────────────────
t1 = std::move(t3);
t1 = { 3, gamma, {6,7,8} }, t3 = { 3, gamma, {} }
────────────────────────────────────────────────────────────
t4 = { delta, {10,11,12} }, p1 = { epsilon, {14,15,16} };
t4 = p1;
t4 = { epsilon, {14,15,16} }, p1 = { epsilon, {14,15,16} };
────────────────────────────────────────────────────────────
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.

DR Applied to Behavior as published Correct behavior
LWG 2729 C++11 tuple::operator= was unconstrained and might
result in unnecessary undefined behavior
constrained

[edit] See also