Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/swap"

From cppreference.com
< cpp‎ | algorithm
(p0185r1)
m
 
(37 intermediate revisions by 16 users not shown)
Line 2: Line 2:
 
{{cpp/algorithm/navbar}}
 
{{cpp/algorithm/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | algorithm | utility | notes={{mark until c++11}}<br>{{mark since c++11}}}}
+
{{dcl header|algorithm|notes={{mark until c++11}}}}
{{dcl | num=1 |
+
{{dcl header|utility|notes={{mark since c++11}}}}
 +
{{dcl header|string_view}}
 +
{{dcl|num=1|notes={{mark|conditionally noexcept since C++11}}<br>{{mark constexpr since c++20}}|1=
 
template< class T >
 
template< class T >
 
void swap( T& a, T& b );
 
void swap( T& a, T& b );
 
}}
 
}}
{{dcl | num=2 | since=c++11 |
+
{{dcl|num=2|notes={{mark|conditionally noexcept since C++11}}<br>{{mark constexpr since c++20}}|1=
template< class T2, size_t N >
+
template< class T2, std::size_t N >
void swap( T2 (&a)[N], T2 (&b)[N]);
+
void swap( T2 (&a)[N], T2 (&b)[N] );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 15: Line 17:
 
Exchanges the given values.
 
Exchanges the given values.
  
@1@ Swaps the values {{tt|a}} and {{tt|b}}. {{rev inl|since=c++17|This overload does not participate is overload resolution unless {{c|std::is_move_constructible_v<T> && std::is_move_assignable_v<T>}}}}
+
@1@ Swaps the values {{c|a}} and {{c|b}}.
 +
{{rrev|since=c++17|
 +
{{cpp/enable if|{{c|std::is_move_constructible_v<T> && std::is_move_assignable_v<T>}} is {{c|true}}}}.
 +
}}
  
@2@ Swaps the arrays {{tt|a}} and {{tt|b}}. In effect calls {{c|std::swap_ranges(a, a+N, b)}}.
+
@2@ Swaps the arrays {{c|a}} and {{c|b}}. Equivalent to {{c|std::swap_ranges(a, a + N, b)}}.
 +
{{rrev|since=c++17|
 +
{{cpp/enable if|{{c|std::is_swappable_v<T2>}} is {{c|true}}}}.
 +
}}
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | a, b | the values to be swapped}}
+
{{par|a, b|the values to be swapped}}
 
{{par hreq}}
 
{{par hreq}}
{{par req concept | T | MoveConstructible | MoveAssignable}}
+
{{par req|{{tt|T}} must meet the requirements of {{rev inl|until=c++11|{{named req|CopyConstructible}} and {{named req|CopyAssignable}}}}{{rev inl|since=c++11|{{named req|MoveConstructible}} and {{named req|MoveAssignable}}}}.}}
{{par req concept | T2 | Swappable }}
+
{{par req named|T2|Swappable}}
 
{{par end}}
 
{{par end}}
  
Line 31: Line 39:
  
 
===Exceptions===
 
===Exceptions===
@1@{{rev begin}}
+
@1@ {{rrev multi|until1=c++11
{{rev|until=c++11|(none)}}
+
|rev1=(none)
{{rev|since=c++11|
+
|rev2=
 
{{noexcept|
 
{{noexcept|
 
     std::is_nothrow_move_constructible<T>::value &&
 
     std::is_nothrow_move_constructible<T>::value &&
 
     std::is_nothrow_move_assignable<T>::value
 
     std::is_nothrow_move_assignable<T>::value
 
}}
 
}}
}}{{rev end}}
+
}}
@2@ {{rev begin}}
+
@2@ {{rrev multi|since1=c++11|rev1=
{{rev|until=c++17|{{noexcept|noexcept(swap(*a, *b))}}}}
+
{{noexcept|noexcept(swap(*a, *b))}} The lookup for the identifier {{tt|swap}} in the exception specification finds this function template in addition to anything found by the usual lookup rules, making the exception specification equivalent to C++17 {{lc|std::is_nothrow_swappable}}.
{{rev|since=c++17|{{noexcept|std::is_nothrow_swappable_v<T>}}}}{{rev end}}
+
|since2=c++17|rev2={{noexcept|std::is_nothrow_swappable_v<T2>}}
 +
}}
  
 
===Complexity===
 
===Complexity===
@1@ Constant
+
@1@ Constant.
  
@2@ Linear in N
+
@2@ Linear in {{c|N}}.
  
 
===Specializations===
 
===Specializations===
 
+
{{rrev|until=c++20|
{{lc|std::swap}} may be [[cpp/language/extending_std|specialized in namespace std]] for user-defined types, but such specializations are not found by [[cpp/language/adl|ADL]] (the namespace std is not the associated namespace for the user-defined type). The expected way to make a user-defined type swappable is to provide a non-member function swap in the same namespace as the type: see {{concept|Swappable}} for details.  
+
{{tt|std::swap}} may be [[cpp/language/extending std|specialized in namespace std]] for program-defined types, but such specializations are not found by [[cpp/language/adl|ADL]] (the namespace std is not the associated namespace for the program-defined type).
 +
}}
 +
The expected way to make a {{lsd|cpp/language/type#Program-defined type}} swappable is to provide a non-member function swap in the same namespace as the type: see {{named req|Swappable}} for details.  
  
 
The following overloads are already provided by the standard library:
 
The following overloads are already provided by the standard library:
 
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/utility/pair/dsc swap2}}
+
{{dsc inc|cpp/utility/pair/dsc swap2}}
{{dsc inc | cpp/utility/tuple/dsc swap2}}
+
{{dsc inc|cpp/utility/tuple/dsc swap2}}
{{dsc inc | cpp/memory/shared_ptr/dsc swap2}}
+
{{dsc inc|cpp/memory/shared_ptr/dsc swap2}}
{{dsc inc | cpp/memory/weak_ptr/dsc swap2}}
+
{{dsc inc|cpp/memory/weak_ptr/dsc swap2}}
{{dsc inc | cpp/memory/unique_ptr/dsc swap2}}
+
{{dsc inc|cpp/memory/unique_ptr/dsc swap2}}
{{dsc inc | cpp/utility/functional/function/dsc swap2}}
+
{{dsc inc|cpp/utility/functional/function/dsc swap2}}
{{dsc inc | cpp/string/basic_string/dsc swap2}}
+
{{dsc inc|cpp/string/basic_string/dsc swap2}}
{{dsc inc | cpp/container/dsc swap2 | array}}
+
{{dsc inc|cpp/container/dsc swap2|array}}
{{dsc inc | cpp/container/dsc swap2 | deque}}
+
{{dsc inc|cpp/container/dsc swap2|deque}}
{{dsc inc | cpp/container/dsc swap2 | forward_list}}
+
{{dsc inc|cpp/container/dsc swap2|forward_list}}
{{dsc inc | cpp/container/dsc swap2 | list}}
+
{{dsc inc|cpp/container/dsc swap2|list}}
{{dsc inc | cpp/container/dsc swap2 | vector}}
+
{{dsc inc|cpp/container/dsc swap2|vector}}
{{dsc inc | cpp/container/dsc swap2 | map}}
+
{{dsc inc|cpp/container/dsc swap2|map}}
{{dsc inc | cpp/container/dsc swap2 | multimap}}
+
{{dsc inc|cpp/container/dsc swap2|multimap}}
{{dsc inc | cpp/container/dsc swap2 | set}}
+
{{dsc inc|cpp/container/dsc swap2|set}}
{{dsc inc | cpp/container/dsc swap2 | multiset}}
+
{{dsc inc|cpp/container/dsc swap2|multiset}}
{{dsc inc | cpp/container/dsc swap2 | unordered_map}}
+
{{dsc inc|cpp/container/dsc swap2|unordered_map}}
{{dsc inc | cpp/container/dsc swap2 | unordered_multimap}}
+
{{dsc inc|cpp/container/dsc swap2|unordered_multimap}}
{{dsc inc | cpp/container/dsc swap2 | unordered_set}}
+
{{dsc inc|cpp/container/dsc swap2|unordered_set}}
{{dsc inc | cpp/container/dsc swap2 | unordered_multiset}}
+
{{dsc inc|cpp/container/dsc swap2|unordered_multiset}}
{{dsc inc | cpp/container/dsc swap2 | queue}}
+
{{dsc inc|cpp/container/dsc swap2|queue}}
{{dsc inc | cpp/container/dsc swap2 | priority_queue}}
+
{{dsc inc|cpp/container/dsc swap2|priority_queue}}
{{dsc inc | cpp/container/dsc swap2 | stack}}
+
{{dsc inc|cpp/container/dsc swap2|stack}}
{{dsc inc | cpp/numeric/valarray/dsc swap2}}
+
{{dsc inc|cpp/numeric/valarray/dsc swap2}}
{{dsc inc | cpp/io/basic_stringbuf/dsc swap2}}
+
{{dsc inc|cpp/io/basic_stringbuf/dsc swap2}}
{{dsc inc | cpp/io/basic_stringstream/dsc swap2 | basic_istringstream}}
+
{{dsc inc|cpp/io/basic_stringstream/dsc swap2|basic_istringstream}}
{{dsc inc | cpp/io/basic_stringstream/dsc swap2 | basic_ostringstream}}
+
{{dsc inc|cpp/io/basic_stringstream/dsc swap2|basic_ostringstream}}
{{dsc inc | cpp/io/basic_stringstream/dsc swap2 | basic_stringstream}}
+
{{dsc inc|cpp/io/basic_stringstream/dsc swap2|basic_stringstream}}
{{dsc inc | cpp/io/basic_filebuf/dsc swap2}}
+
{{dsc inc|cpp/io/basic_filebuf/dsc swap2}}
{{dsc inc | cpp/io/basic_fstream/dsc swap2 | basic_ifstream}}
+
{{dsc inc|cpp/io/basic_fstream/dsc swap2|basic_ifstream}}
{{dsc inc | cpp/io/basic_fstream/dsc swap2 | basic_ofstream}}
+
{{dsc inc|cpp/io/basic_fstream/dsc swap2|basic_ofstream}}
{{dsc inc | cpp/io/basic_fstream/dsc swap2 | basic_fstream}}
+
{{dsc inc|cpp/io/basic_fstream/dsc swap2|basic_fstream}}
{{dsc inc | cpp/regex/basic_regex/dsc swap2}}
+
{{dsc inc|cpp/io/basic_syncbuf/dsc swap2}}
{{dsc inc | cpp/regex/match_results/dsc swap2}}
+
{{dsc inc|cpp/io/basic_spanbuf/dsc swap2}}
{{dsc inc | cpp/thread/thread/dsc swap2}}
+
{{dsc inc|cpp/io/basic_spanstream/dsc swap2|basic_ispanstream}}
{{dsc inc | cpp/thread/unique_lock/dsc swap2}}
+
{{dsc inc|cpp/io/basic_spanstream/dsc swap2|basic_ospanstream}}
{{dsc inc | cpp/thread/promise/dsc swap2}}
+
{{dsc inc|cpp/io/basic_spanstream/dsc swap2|basic_spanstream}}
{{dsc inc | cpp/thread/packaged_task/dsc swap2}}
+
{{dsc inc|cpp/regex/basic_regex/dsc swap2}}
 +
{{dsc inc|cpp/regex/match_results/dsc swap2}}
 +
{{dsc inc|cpp/thread/thread/dsc swap2|thread}}
 +
{{dsc inc|cpp/thread/unique_lock/dsc swap2}}
 +
{{dsc inc|cpp/thread/shared_lock/dsc swap2}}
 +
{{dsc inc|cpp/thread/promise/dsc swap2}}
 +
{{dsc inc|cpp/thread/packaged_task/dsc swap2}}
 +
{{dsc inc|cpp/utility/optional/dsc swap2}}
 +
{{dsc inc|cpp/utility/any/dsc swap2}}
 +
{{dsc inc|cpp/utility/variant/dsc swap2}}
 +
{{dsc inc|cpp/utility/basic_stacktrace/dsc swap2}}
 +
{{dsc inc|cpp/filesystem/path/dsc swap2}}
 +
{{dsc inc|cpp/utility/expected/dsc swap2}}
 +
{{dsc inc|cpp/thread/thread/dsc swap2|jthread}}
 +
{{dsc inc|cpp/utility/functional/move_only_function/dsc swap2}}
 +
{{dsc inc|cpp/thread/stop_source/dsc swap2}}
 +
{{dsc inc|cpp/thread/stop_token/dsc swap2}}
 +
<!--TODOs:
 +
swap(std::flat_map)
 +
swap(std::flat_set)
 +
swap(std::flat_multimap)
 +
swap(std::flat_multiset)
 +
swap(std::unexpected)
 +
swap(std::mdspan)
 +
-->
 
{{dsc end}}
 
{{dsc end}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| <!-- should demo proper use of swap with generic types, as in void swap(T& other) {using std::swap; swap(member, other.member);} , not just trivialities -->
+
|<!-- should demo proper use of swap with generic types, as in void swap(T& other) {using std::swap; swap(member, other.member);} , not just trivialities -->
| code=
+
|code=
 
#include <algorithm>
 
#include <algorithm>
 
#include <iostream>
 
#include <iostream>
  
int main()
+
namespace Ns
 
{
 
{
  int a = 5, b = 3;
+
    class A
 
+
    {
  // before
+
        int id {};
  std::cout << a << ' ' << b << '\n';
+
       
 +
        friend void swap(A& lhs, A& rhs)
 +
        {
 +
            std::cout << "swap(" << lhs << ", " << rhs << ")\n";
 +
            std::swap(lhs.id, rhs.id);
 +
        }
 +
       
 +
        friend std::ostream& operator<<(std::ostream& os, A const& a)
 +
        {
 +
            return os << "A::id=" << a.id;
 +
        }
 +
   
 +
    public:
 +
        A(int i) : id {i} {}
 +
        A(A const&) = delete;
 +
        A& operator = (A const&) = delete;
 +
    };
 +
}
  
  std::swap(a,b);
+
int main()
 
+
{
  // after
+
    int a = 5, b = 3;
  std::cout << a << ' ' << b << '\n';
+
    std::cout << a << ' ' << b << '\n';
 +
    std::swap(a, b);
 +
    std::cout << a << ' ' << b << '\n';
 +
   
 +
    Ns::A p {6}, q {9};
 +
    std::cout << p << ' ' << q << '\n';
 +
// std::swap(p, q); // error, type requirements are not satisfied
 +
    swap(p, q);      // OK, ADL finds the appropriate friend `swap`
 +
    std::cout << p << ' ' << q << '\n';
 
}
 
}
| output=
+
|output=
 
5 3
 
5 3
 
3 5
 
3 5
 +
A::id=6 A::id=9
 +
swap(A::id=6, A::id=9)
 +
A::id=9 A::id=6
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=227|std=C++98|before={{tt|T}} was not required to be {{named req|CopyConstructible}} or {{named req|DefaultConstructible}}<br>(a temporary object of type {{tt|T}} might not be able to be constructed)|after={{tt|T}} is also required to<br>be {{named req|CopyConstructible}}}}
 +
{{dr list item|wg=lwg|dr=809|std=C++98|before=arrays could not be swapped|after=added overload {{v|2}}}}
 +
{{dr list item|wg=lwg|dr=2554|std=C++11|before=swapping multi-dimensional arrays can never<br>be {{c/core|noexcept}} due to name lookup problems|after=made to work}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc iter_swap}}
+
{{dsc inc|cpp/utility/ranges/dsc swap}}
{{dsc inc | cpp/algorithm/dsc swap_ranges}}
+
{{dsc inc|cpp/algorithm/dsc iter_swap}}
 +
{{dsc inc|cpp/algorithm/dsc swap_ranges}}
 +
{{dsc inc|cpp/utility/dsc exchange}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/algorithm/swap]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/algorithm/swap]]
+
[[fr:cpp/algorithm/swap]]
+
[[it:cpp/algorithm/swap]]
+
[[ja:cpp/algorithm/swap]]
+
[[pt:cpp/algorithm/swap]]
+
[[ru:cpp/algorithm/swap]]
+
[[zh:cpp/algorithm/swap]]
+

Latest revision as of 19:32, 1 September 2024

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
swap
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Defined in header <algorithm>
(until C++11)
Defined in header <utility>
(since C++11)
Defined in header <string_view>
template< class T >
void swap( T& a, T& b );
(1) (conditionally noexcept since C++11)
(constexpr since C++20)
template< class T2, std::size_t N >
void swap( T2 (&a)[N], T2 (&b)[N] );
(2) (conditionally noexcept since C++11)
(constexpr since C++20)

Exchanges the given values.

1) Swaps the values a and b.

This overload participates in overload resolution only if std::is_move_constructible_v<T> && std::is_move_assignable_v<T> is true.

(since C++17)
2) Swaps the arrays a and b. Equivalent to std::swap_ranges(a, a + N, b).

This overload participates in overload resolution only if std::is_swappable_v<T2> is true.

(since C++17)

Contents

[edit] Parameters

a, b - the values to be swapped
Type requirements
-
T must meet the requirements of CopyConstructible and CopyAssignable(until C++11)MoveConstructible and MoveAssignable(since C++11).
-
T2 must meet the requirements of Swappable.

[edit] Return value

(none)

[edit] Exceptions

1)

(none)

(until C++11)
noexcept specification:  
noexcept(

    std::is_nothrow_move_constructible<T>::value &&
    std::is_nothrow_move_assignable<T>::value

)
(since C++11)
2)
noexcept specification:  
noexcept(noexcept(swap(*a, *b)))
The lookup for the identifier swap in the exception specification finds this function template in addition to anything found by the usual lookup rules, making the exception specification equivalent to C++17 std::is_nothrow_swappable.
(since C++11)
(until C++17)
noexcept specification:  
(since C++17)

[edit] Complexity

1) Constant.
2) Linear in N.

[edit] Specializations

std::swap may be specialized in namespace std for program-defined types, but such specializations are not found by ADL (the namespace std is not the associated namespace for the program-defined type).

(until C++20)

The expected way to make a program-defined type swappable is to provide a non-member function swap in the same namespace as the type: see Swappable for details.

The following overloads are already provided by the standard library:

specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function) [edit]
specializes the std::swap algorithm
(function) [edit]
specializes the std::swap algorithm
(function) [edit]
specializes the std::swap algorithm
(function) [edit]
specializes the std::swap algorithm
(function) [edit]
specializes the std::swap algorithm
(function) [edit]

[edit] Example

#include <algorithm>
#include <iostream>
 
namespace Ns
{
    class A
    {
        int id {};
 
        friend void swap(A& lhs, A& rhs)
        {
            std::cout << "swap(" << lhs << ", " << rhs << ")\n";
            std::swap(lhs.id, rhs.id);
        }
 
        friend std::ostream& operator<<(std::ostream& os, A const& a)
        {
            return os << "A::id=" << a.id;
        }
 
    public:
        A(int i) : id {i} {}
        A(A const&) = delete;
        A& operator = (A const&) = delete;
    };
}
 
int main()
{
    int a = 5, b = 3;
    std::cout << a << ' ' << b << '\n';
    std::swap(a, b);
    std::cout << a << ' ' << b << '\n';
 
    Ns::A p {6}, q {9};
    std::cout << p << ' ' << q << '\n';
//  std::swap(p, q); // error, type requirements are not satisfied
    swap(p, q);      // OK, ADL finds the appropriate friend `swap`
    std::cout << p << ' ' << q << '\n';
}

Output:

5 3
3 5
A::id=6 A::id=9
swap(A::id=6, A::id=9)
A::id=9 A::id=6

[edit] 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 227 C++98 T was not required to be CopyConstructible or DefaultConstructible
(a temporary object of type T might not be able to be constructed)
T is also required to
be CopyConstructible
LWG 809 C++98 arrays could not be swapped added overload (2)
LWG 2554 C++11 swapping multi-dimensional arrays can never
be noexcept due to name lookup problems
made to work

[edit] See also

swaps the values of two objects
(customization point object)[edit]
swaps the elements pointed to by two iterators
(function template) [edit]
swaps two ranges of elements
(function template) [edit]
(C++14)
replaces the argument with a new value and returns its previous value
(function template) [edit]