cpp / latest / utility / tuple / operator=.html /

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

(1)
tuple& operator=( const tuple& other );
(since C++11)
(until C++20)
constexpr tuple& operator=( const tuple& other );
(since C++20)
constexpr const tuple& operator=( const tuple& other ) const;
(2) (since C++23)
(3)
tuple& operator=( tuple&& other ) noexcept(/* see below */);
(since C++11)
(until C++20)
constexpr tuple& operator=( tuple&& other ) noexcept(/* see below */);
(since C++20)
constexpr const tuple& operator=( tuple&& other ) const;
(4) (since C++23)
(5)
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)
template< class... UTypes >
constexpr const tuple& operator=( const tuple<UTypes...>& other ) const;
(6) (since C++23)
(7)
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)
template< class... UTypes >
constexpr const tuple& operator=( tuple<UTypes...>&& other ) const;
(8) (since C++23)
(9)
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)
template< class U1, class U2 >
constexpr const tuple& operator=( const std::pair<U1, U2>& p ) const;
(10) (since C++23)
(11)
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)
template< class U1, class U2 >
constexpr const tuple& operator=( std::pair<U1, U2>&& p ) const;
(12) (since C++23)

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

Parameters

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

Return value

*this.

Exceptions

1,2) May throw implementation-defined exceptions.
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-12) May throw implementation-defined exceptions.

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... Args>
Os& operator<< (Os& os, const std::tuple<Args...>& t) {
    os << "{ ";
    std::apply([&](auto&& arg, auto&&... args) {
        os << arg;
        ((os << ", " << args), ...);
    }, 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() {
    // Tuple to tuple examples //
    std::tuple<int, std::string, std::vector<int>>
        t1{1, "alpha", {1, 2, 3} },
        t2{2, "beta", {4, 5} };
 
    // Normal copy assignment
    // operator=( const tuple& other );
    std::cout << "t1 = " << t1 << ", t2 = " << t2 << '\n';
    t1 = t2;
    std::cout << "t1 = t2;\n" "t1 = " << t1 << ", t2 = " << t2 << '\n' << line{};
 
    // Normal move assignment
    // operator=( tuple&& other );
    t1 = std::move(t2);
    std::cout << "t1 = std::move(t2);\n" "t1 = " << t1 << ", t2 = " << t2 << '\n' << line{};
 
    // Converting copy assignment
    // operator=( const tuple<UTypes...>& other );
    std::tuple<short, const char*, std::vector<int>> t3{3, "gamma", {6,7,8} };
    t1 = t3;
    std::cout << "t1 = t3; \n" "t1 = " << t1 << ", t3 = " << t3 << '\n' << line{};
 
    // Converting move assignment
    // operator=( tuple<UTypes...>&& other );
    t1 = std::move(t3);
    std::cout << "t1 = std::move(t3);\n" "t1 = " << t1 << ", t3 = " << t3 << '\n' << line{};
 
    // Pair to tuple examples //
    std::tuple<std::string, std::vector<int>> t4{"delta", {10,11,12} };
    std::pair<const char*, std::vector<int>> p1{"epsilon", {14,15,16} };
 
    // Converting copy assignment from std::pair
    // operator=( const std::pair<U1,U2>& p );
    std::cout << "t4 = " << t4 << ", "
              << "p1 = { " << p1.first << ", " << p1.second << " };\n";
    t4 = p1;
    std::cout << "t4 = p1;\n" "t4 = " << t4
              << ", p1 = { " << p1.first << ", " << p1.second << " };\n" << line{};
 
    // Converting move assignment from std::pair
    // operator=( std::pair<U1,U2>&& p );
    t4 = std::move(p1);
    std::cout << "t4 = std::move(p1);\n" "t4 = " << t4
              << ", p1 = { " << p1.first << ", " << p1.second << " };\n" << line{};
 
#ifdef __cpp_lib_ranges_zip
    // Const tuple-of-proxies assignment example
    std::vector<bool> v({false, true});
    const std::tuple<std::vector<bool>::reference> t0_const{v[0]}, t1_const{v[1]};
    t0_const = t1_const;
    std::cout << std::boolalpha << "t0_const = t1_const;\n" "t0_const = " << t0_const
              << ", t1_const = " << t1_const << '\n';
#endif
}

Possible 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, {} };
────────────────────────────────────────────────────────────
t0_const = t1_const;
t0_const = { true }, t1_const = { true }

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

See also

(C++11)
constructs a new tuple
(public member function)
assigns the contents
(public member function of std::pair<T1,T2>)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/tuple/operator%3D