Skip to content
Tutorials Center
My learning

Topics

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.

C++
std::swap(a, b);
auto old = std::exchange(state, State::Done);

Try it

C++ Swapping values, containers and arrays

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.

C++ std::exchange and the ADL swap idiom

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

C++
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 plain std::swap(a, b) ignores your custom overload. std::ranges::swap does 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 swap that runs in constant time (except std::array, which is linear). Iterators and references to elements generally stay valid, and now refer to elements in the other container.
  • std::exchange is ideal for move constructors and for "take the value and reset" patterns, such as auto old = std::exchange(callback, nullptr);.
  • Order of evaluation: exchange is 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::swap on two unique_ptrs swaps the owned pointers. Swapping std::string with the small string optimization copies the characters.
  • std::swap_ranges swaps whole ranges; std::iter_swap is the iterator form and is what sorting algorithms use.