Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/tuple/swap2"

From cppreference.com
< cpp‎ | utility‎ | tuple
m (fmt, {{c}})
 
(14 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{cpp/title | swap<small>(std::tuple)</small>}}
+
{{cpp/title|swap<small>(std::tuple)</small>}}
{{cpp/utility/tuple/sidebar}}
+
{{cpp/utility/tuple/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list item | notes={{mark c++11 feature}} |
+
{{dcl header|tuple}}
 +
{{dcl rev multi|num=1|since1=c++11|dcl1=
 
template< class... Types >
 
template< class... Types >
void swap( tuple<Types...>& lhs, tuple<Types...>& rhs );
+
void swap( std::tuple<Types...>& lhs,
 +
          std::tuple<Types...>& rhs ) noexcept(/* see below */);
 +
|since2=c++20|dcl2=
 +
template< class... Types >
 +
constexpr void swap( std::tuple<Types...>& lhs,
 +
                    std::tuple<Types...>& rhs ) noexcept(/* see below */);
 +
}}
 +
{{dcl|num=2|since=c++23|
 +
template< class... Types >
 +
constexpr void swap( const std::tuple<Types...>& lhs,
 +
                    const std::tuple<Types...>& rhs ) noexcept(/* see below */);
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Swaps the contents of {{tt|lhs}} and {{tt|rhs}}. Equivalent to {{cpp|lhs.swap(rhs)}}.
+
Swaps the contents of {{c|lhs}} and {{c|rhs}}. Equivalent to {{c|lhs.swap(rhs)}}.
 +
 
 +
{{rrev|since=c++17|
 +
@1@ {{cpp/enable_if|{{c|std::is_swappable_v<Ti>}} is {{c|true}} for all i from 0 to {{c|sizeof...(Types)}}}}.
 +
@2@ {{cpp/enable_if|{{c|std::is_swappable_v<const Ti>}} is {{c|true}} for all i from 0 to {{c|sizeof...(Types)}}}}.
 +
}}
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | lhs, rhs | tuples whose contents to swap}}
+
{{par|lhs, rhs|tuples whose contents to swap}}
{{param list end}}  
+
{{par end}}  
  
 
===Return value===
 
===Return value===
Line 20: Line 36:
 
===Exceptions===
 
===Exceptions===
 
{{noexcept|noexcept(lhs.swap(rhs))}}
 
{{noexcept|noexcept(lhs.swap(rhs))}}
 +
 +
===Example===
 +
{{example_std_tuple_swap}}
  
 
===See also===
 
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/utility/tuple/dsc swap}}
 +
{{dsc inc|cpp/utility/pair/dsc swap2}}
 +
{{dsc end}}
  
{{dcl list begin}}
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
{{dcl list end}}
+

Latest revision as of 06:38, 14 October 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <tuple>
(1)
template< class... Types >

void swap( std::tuple<Types...>& lhs,

           std::tuple<Types...>& rhs ) noexcept(/* see below */);
(since C++11)
(until C++20)
template< class... Types >

constexpr void swap( std::tuple<Types...>& lhs,

                     std::tuple<Types...>& rhs ) noexcept(/* see below */);
(since C++20)
template< class... Types >

constexpr void swap( const std::tuple<Types...>& lhs,

                     const std::tuple<Types...>& rhs ) noexcept(/* see below */);
(2) (since C++23)

Swaps the contents of lhs and rhs. Equivalent to lhs.swap(rhs).

1) This overload participates in overload resolution only if std::is_swappable_v<Ti> is true for all i from 0 to sizeof...(Types).
2) This overload participates in overload resolution only if std::is_swappable_v<const Ti> is true for all i from 0 to sizeof...(Types).
(since C++17)

Contents

[edit] Parameters

lhs, rhs - tuples whose contents to swap

[edit] Return value

(none)

[edit] Exceptions

noexcept specification:  
noexcept(noexcept(lhs.swap(rhs)))

[edit] Example

#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}

[edit] See also

swaps the contents of two tuples
(public member function) [edit]
specializes the std::swap algorithm
(function template) [edit]