std::tuple::operator=(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME πŸ’…

std::tuple::operator= β€” literally the assignment operator for std::tuple, no cap. This is std::tuple::operator= from the C++ Standard Library manual, page std::tuple::operator=(3). πŸ¦‹


✨ Synopsis ✨

okay bestie, sit down, hydrate, and get comfy, because there are fourteen overloads here and we're gonna go through every single one. 🫢

tuple& operator=( const tuple& other );                            (1)  (since C++11)
                                                                         (constexpr since C++20)
constexpr const 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)
constexpr const tuple& operator=( tuple&& other ) const;           (4)  (since C++23)
template< class... UTypes >                                        (5)  (since C++11)
tuple& operator=( const tuple<UTypes...>& other );
                                                                         (constexpr since C++20)
template< class... UTypes >
constexpr const tuple& operator=( const tuple<UTypes...>& other ) const;  (6)  (since C++23)
template< class... UTypes >                                        (7)  (since C++11)
tuple& operator=( tuple<UTypes...>&& other );
                                                                         (constexpr since C++20)
template< class... UTypes >
constexpr const tuple& operator=( tuple<UTypes...>&& other ) const; (8)  (since C++23)
template< class E1, class E2 >                                     (9)  (since C++11)
tuple& operator=( const std::pair<E1, E2>& p );
                                                                         (constexpr since C++20)
template< class E1, class E2 >
constexpr const tuple& operator=( const std::pair<E1, E2>& p ) const;  (10) (since C++23)
template< class E1, class E2 >                                     (11) (since C++11)
tuple& operator=( std::pair<E1, E2>&& p );
                                                                         (constexpr since C++20)
template< class E1, class E2 >
constexpr const tuple& operator=( std::pair<E1, E2>&& p ) const;   (12) (since C++23)
template< tuple-like UTuple >                                      (13) (since C++23)
constexpr tuple& operator=( UTuple&& u );
template< tuple-like UTuple >                                      (14) (since C++23)
constexpr const tuple& operator=( UTuple&& u ) const;

so what's the vibe? it replaces the contents of the tuple with the contents of another tuple-like object. iconic behavior honestly. πŸ”₯

and before we get into the descriptions, let's define our little squad of indices, because the rest of this page is obsessed with them:

  • i is in the range [0, sizeof...(Types)), in order,
  • Ti is the ith type in the class template parameter pack Types, and
  • Ui is the ith type in a function template parameter pack named UTypes,

and indexing is zero-based. (as it should be. periodt. πŸ’…)


πŸŽ€ the fourteen overloads, one by one

  1. Copy assignment operator. Assigns each element of other to the corresponding element of *this. This overload is defined as deleted unless std::is_copy_assignable<Ti>::value is true for all Ti. 😭 (so if any element type refuses to be copy-assigned, this whole overload just ✨does not exist✨ for you)
  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 Ti> is true for all Ti. πŸ‘‘
  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<Ti>::value is true for all Ti. πŸƒβ€β™€οΈπŸ’¨
  4. Move assignment operator for 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 Ti&, Ti> is true for all Ti.
  5. For all i, assigns std::get<i>(other) to std::get<i>(*this). This overload participates in overload resolution only if sizeof...(Types) == sizeof...(UTypes), and std::is_assignable<Ti&, const Ui&>::value is true for all corresponding pairs of types Ti and Ui. 🧚 (converting copy assignment, we stan)
  6. For all i, assigns std::get<i>(other) to std::get<i>(*this). This overload participates in overload resolution only if sizeof...(Types) == sizeof...(UTypes), and std::is_assignable_v<const Ti&, const Ui&> is true for all corresponding pairs of types Ti and Ui.
  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...(Types) == sizeof...(UTypes), and std::is_assignable<Ti&, Ui>::value is true for all corresponding pairs of types Ti and Ui. πŸ’«
  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...(Types) == sizeof...(UTypes), and std::is_assignable_v<const Ti&, Ui> is true for all corresponding pairs of types Ti and Ui.
  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<T0&, const E1&>::value is true, and
    • std::is_assignable<T1&, const E2&>::value is true.

πŸ“ pair β†’ tuple, converting copy. cute.

  1. Assigns p.first to the first element and p.second to the second element. This overload participates in overload resolution only if
    • sizeof...(Types) == 2,
    • std::is_assignable_v<const T0&, const E1&> is true, and
    • std::is_assignable_v<const T1&, const E2&> is true.
  2. Assigns std::forward<E1>(p.first) to the first element of *this and std::forward<E2>(p.second) to the second element of *this. This overload participates in overload resolution only if
    • sizeof...(Types) == 2,
    • std::is_assignable_v<T0&, E1> is true, and
    • std::is_assignable_v<T1&, E2> is true.
  3. Assigns std::forward<E1>(p.first) to the first element and std::forward<E2>(p.second) to the second element. This overload participates in overload resolution only if
    • sizeof...(Types) == 2,
    • std::is_assignable_v<const T0&, E1> is true, and
    • std::is_assignable_v<const T1&, E2> is true.
  4. For all i, assigns std::get<i>(std::forward<UTuple>(u)) to std::get<i>(*this). This overload participates in overload resolution only if
    • std::same_as<std::remove_cvref_t<UTuple>, std::tuple> is false,
    • std::remove_cvref_t<UTuple> is not a specialization of std::ranges::subrange,
    • sizeof...(Types) equals std::tuple_size_v<std::remove_cvref_t<UTuple>>, and
    • std::is_assignable_v<Ti&, decltype(std::get<i>(std::forward<UTuple>(u)))> is true for all i.

🌈 the shiny new C++23 tuple-like overload, main character energy fr.

  1. For all i, assigns std::get<i>(std::forward<UTuple>(u)) to std::get<i>(*this). This overload participates in overload resolution only if
    • std::same_as<std::remove_cvref_t<UTuple>, std::tuple> is false,
    • std::remove_cvref_t<UTuple> is not a specialization of std::ranges::subrange,
    • sizeof...(Types) equals std::tuple_size_v<std::remove_cvref_t<UTuple>>, and
    • std::is_assignable_v<const Ti&, decltype(std::get<i>(std::forward<UTuple>(u)))> is true for all i.

🫢 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

πŸ’– Return value πŸ’–

*this β€” that's it, that's the return value. she gives back herself. self-love icon. πŸ‘‘


πŸ”₯ Exceptions πŸ”₯

1, 2) May throw implementation-defined exceptions if the assignment of one of the types in Types 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 exceptions if the assignment of one of the types in Types throws an exception.


πŸŽ€ 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 size
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()
{
    // 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, const char*, 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<const char*, 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 ✨ (literally the exact possible output, no edits, no cap):

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

πŸ’€ Defect reports πŸ’€

Okay so these are the behavior-changing defect reports that got applied retroactively to previously published C++ standards. The standard really said β€œreceipts” and meant it:

  • LWG 2729 β€” applied to C++11. Behavior as published: operator= was unconstrained and might result in unnecessary undefined behavior. Correct behavior: constrained.

πŸ‘€ See also πŸ‘€

  • constructor β€” constructs a new tuple (public member function)
  • operator= β€” assigns the contents (public member function of std::pair<T1,T2>)

Category: * conditionally noexcept


That’s the whole vibe from cppreference.com, version/date 2024.06.10, page std::tuple::operator=(3) β€” go assign something iconic, bestie πŸ’…βœ¨πŸ«Ά

πŸ“¦ where this page comes from