|
|
Member functions | ||||
tuple::swap | ||||
Non-member functions | ||||
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20) | ||||
Helper concepts | ||||
(C++23) | ||||
Helper classes | ||||
(C++23) | ||||
(C++23) | ||||
Deduction guides(C++17) |
Defined in header <tuple> | ||
void swap( tuple& other)noexcept(/* see below */); | (1) | (since C++11) (constexpr since C++20) |
constexprvoid swap(const tuple& other)noexcept(/* see below */)const; | (2) | (since C++23) |
Callsswap
(which might bestd::swap, or might be found viaADL) for each element in*this and its corresponding element inother.
If any selected | (until C++23) |
If any selected | (since C++23) |
Contents |
other | - | tuple of values to swap |
(none)
noexcept specification: noexcept( noexcept(swap(std::declval<T0&>>(),std::declval<T0&>()))&& In the expression above, the identifier | (until C++17) |
1) noexcept specification: noexcept((std::is_nothrow_swappable_v<Types>&& ...)) 2) noexcept specification: noexcept((std::is_nothrow_swappable_v<const Types>&& ...)) | (since C++17) |
#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}
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2456 | C++11 | thenoexcept specification is ill-formed | made to work |
(C++11) | specializes thestd::swap algorithm (function template)[edit] |
(C++11) | swaps the contents (public member function of std::pair<T1,T2> )[edit] |