Difference between revisions of "cpp/utility/tuple/swap2"
From cppreference.com
m (→See also: + crosslink) |
m (→Example: + shared example) |
||
Line 29: | Line 29: | ||
===Exceptions=== | ===Exceptions=== | ||
{{noexcept|noexcept(lhs.swap(rhs))}} | {{noexcept|noexcept(lhs.swap(rhs))}} | ||
+ | |||
+ | ===Example=== | ||
+ | {{example_std_tuple_swap}} | ||
===See also=== | ===See also=== |
Revision as of 01:48, 7 June 2021
Defined in header <tuple>
|
||
template< class... Types > void swap( tuple<Types...>& lhs, tuple<Types...>& rhs ) noexcept(/* see below */); |
(since C++11) (until C++20) |
|
template< class... Types > constexpr void |
(since C++20) | |
Swaps the contents of lhs
and rhs
. Equivalent to lhs.swap(rhs).
This function does not participate in overload resolution unless std::is_swappable_v<Ti> is true for all i from 0 to |
(since C++17) |
Contents |
Parameters
lhs, rhs | - | tuples whose contents to swap |
Return value
(none)
Exceptions
noexcept specification:
noexcept(noexcept(lhs.swap(rhs)))
Example
Run this code
#include <iostream> #include <string> #include <tuple> int main() { std::tuple<int, std::string, float> p1{42, "ABCD", 2.71}, p2; p2 = std::make_tuple(10, "1234", 3.14); auto print_p1_p2 = [&](auto rem) { std::cout << rem << "p1 = {" << std::get<0>(p1) << ", " << std::get<1>(p1) << ", " << std::get<2>(p1) << "}, " << "p2 = {" << std::get<0>(p2) << ", " << std::get<1>(p2) << ", " << std::get<2>(p2) << "}\n"; }; print_p1_p2("Before p1.swap(p2): "); p1.swap(p2); print_p1_p2("After p1.swap(p2): "); swap(p1, p2); print_p1_p2("After swap(p1, p2): "); }
Output:
Before p1.swap(p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14} After p1.swap(p2): p1 = {10, 1234, 3.14}, p2 = {42, ABCD, 2.71} After swap(p1, p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14}
See also
swaps the contents of two tuple s (public member function) |