table of contents
std::tuple::operator=(3) | C++ Standard Libary | std::tuple::operator=(3) |
NAME¶
std::tuple::operator= - std::tuple::operator=
Synopsis¶
(since
tuple& operator=( const C++11)
tuple& other ); (until
C++20)
constexpr tuple& operator=( (since
const tuple& other ); C++20)
constexpr const tuple& (since
operator=( const tuple& (2) C++23)
other ) const;
tuple& operator=( tuple&& (since
other ) noexcept(/* see C++11)
below */); (until
C++20)
constexpr tuple& operator=( (since
tuple&& other ) noexcept(/* C++20)
see below */);
constexpr const tuple& (since
operator=( tuple&& other ) (4) C++23)
const;
template< class... UTypes > (since
tuple& operator=( const C++11)
tuple<UTypes...>& other ); (until
C++20)
template< class... UTypes >
constexpr tuple& operator=( (since
const tuple<UTypes...>& C++20)
other );
template< class... UTypes >
constexpr const tuple& (since
operator=( const (6) C++23)
tuple<UTypes...>& other )
const;
template< class... UTypes > (since
tuple& operator=( C++11)
tuple<UTypes...>&& other ); (until
(1) C++20)
template< class... UTypes > (since
constexpr tuple& operator=( C++20)
tuple<UTypes...>&& other );
template< class... UTypes >
constexpr const tuple& (3) (since
operator=( (8) C++23)
tuple<UTypes...>&& other )
const;
template< class U1, class U2 (since
> (5) C++11)
tuple& operator=( const (until
std::pair<U1, U2>& p ); C++20)
template< class U1, class U2
> (since
constexpr tuple& operator=( C++20)
const std::pair<U1, U2>& p (7)
);
template< class U1, class U2
>
constexpr const tuple& (10) (since
operator=( const C++23)
std::pair<U1, U2>& p ) (9)
const;
template< class U1, class U2 (since
> C++11)
tuple& operator=( (until
std::pair<U1, U2>&& p ); C++20)
template< class U1, class U2
> (since
constexpr tuple& operator=( (11) C++20)
std::pair<U1, U2>&& p );
template< class U1, class U2
> (since
constexpr const tuple& (12) C++23)
operator=( std::pair<U1,
U2>&& p ) const;
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.
* This overload is defined as deleted unless
std::is_copy_assignable<T_i>::value
is true for all T_i in Types.
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¶
// Run this code
#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 constrained
result in unnecessary undefined behavior
See also¶
constructor constructs a new tuple
(C++11) (public member function)
operator= assigns the contents
(public member function of std::pair<T1,T2>)
2022.07.31 | http://cppreference.com |