std::Swappable, std::SwappableWith

< cpp‎ | concepts
Defined in header <concepts>
template< class T >
concept Swappable = std::is_swappable_v<T>;
(1) (since C++20)
template< class T, class U >

concept SwappableWith =
  std::is_swappable_with_v<T, T> &&
  std::is_swappable_with_v<U, U> &&
    const std::remove_reference_t<T>&,
    const std::remove_reference_t<U>&> &&
  std::is_swappable_with_v<T, U> &&

  std::is_swappable_with_v<U, T>;
(2) (since C++20)

The concept Swappable<T> specifies that lvalues of type T are swappable. The concept SwappableWith<T, U> specifies that expressions of the type and value category encoded by T and U are swappable with each other.

In each case, overload resolution for the call to swap is performed on a candidate set that includes:

These concepts are satisfied only if the call to swap actually exchanges the values of its operands. Formally, given

  • a1, a2, b1 and b2, distinct objects of type T such that a1 is equal to a2 and b1 is equal to b2,

Swappable<T> is satisfied only if, after evaluating either swap(a1, b1) or swap(b1, a1), a1 is equal to b2 and b1 is equal to a2.

And given

SwappableWith<T, U> is satisfied only if, after evaluating either swap(e_t, e_u) or swap(e_u, e_t), C(t1) is equal to C(u2) and C(u1) is equal to C(t2).


These definitions of Swappable and SwappableWith are expected to be temporary, and will be replaced if the full Ranges proposal is adopted for C++20.