C++ Reference ยท Memory and Utilities
C++ std::swap, std::exchange and iter_swap
Reference for std::swap, std::exchange and std::iter_swap: swapping values, the swap-through-ADL idiom, noexcept, move constructors and common use cases.
std::swap(a, b) exchanges the values of two objects. std::exchange(obj, new_value) assigns a new value to an object and returns the old one in a single expression. Both are in <utility> (swap for arrays is also found via <algorithm>). They are building blocks of move operations, the copy-and-swap idiom and many algorithms.
The generic swap uses one move construction and two move assignments, so it is cheap for types with efficient moves. Many standard types also have a member swap that just swaps internal pointers, which does not allocate.
std::swap(a, b); auto old = std::exchange(state, State::Done);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class T> constexpr void swap(T& a, T& b) noexcept(/* see below */); // C++20 constexpr template<class T, size_t N> constexpr void swap(T (&a)[N], T (&b)[N]) noexcept(/* ... */); template<class T, class U = T> constexpr T exchange(T& obj, U&& new_value); // C++14 template<class It1, class It2> constexpr void iter_swap(It1 a, It2 b);
| Name | Description |
|---|---|
a, b |
Objects to swap. They must be move constructible and move assignable. |
obj |
Object that receives the new value. |
new_value |
Value assigned to obj; may be of a different, convertible type. |
Returns: swap returns nothing; exchange returns the previous value of obj (by value). Errors: swap is noexcept only if the move constructor and move assignment are.
Usage notes
- The ADL idiom: to support user-defined swaps in generic code write
using std::swap; swap(a, b);. A plainstd::swap(a, b)ignores your custom overload.std::ranges::swapdoes the lookup for you. - Provide your own swap next to your type (as a friend or in its namespace) when a cheaper exchange than three moves exists, and mark it
noexcept. - Standard containers have member
swapthat runs in constant time (exceptstd::array, which is linear). Iterators and references to elements generally stay valid, and now refer to elements in the other container. std::exchangeis ideal for move constructors and for "take the value and reset" patterns, such asauto old = std::exchange(callback, nullptr);.- Order of evaluation:
exchangeis a function call, so the old value is saved before the new one is stored; no temporaries are needed in your code. - Copy-and-swap implements assignment with strong exception safety:
T& operator=(T other) noexcept { swap(*this, other); return *this; }. std::swapon twounique_ptrs swaps the owned pointers. Swappingstd::stringwith the small string optimization copies the characters.std::swap_rangesswaps whole ranges;std::iter_swapis the iterator form and is what sorting algorithms use.