[concept.swappable] (original) (raw)

18 Concepts library [concepts]

18.4.9 Concept swappable [concept.swappable]

Let t1 and t2 be equality-preserving expressions that denote distinct equal objects of type T, and let u1 and u2similarly denote distinct equal objects of type U.

[ Note

:

t1 and u1 can denote distinct objects, or the same object.

end note

]

An operationexchanges the values denoted by t1 and u1 if and only if the operation modifies neither t2 nor u2 and:

The expressionranges​::​swap(E1, E2) for subexpressions E1and E2 is expression-equivalent to an expressionS determined as follows:

[ Note

:

Whenever ranges​::​swap(E1, E2) is a valid expression, it exchanges the values denoted byE1 and E2 and has type void.

end note

]

template<class T> concept swappable = requires(T& a, T& b) { ranges::swap(a, b); };

template<class T, class U> concept swappable_­with = common_reference_with<T, U> && requires(T&& t, U&& u) { ranges::swap(std::forward<T>(t), std::forward<T>(t)); ranges::swap(std::forward<U>(u), std::forward<U>(u)); ranges::swap(std::forward<T>(t), std::forward<U>(u)); ranges::swap(std::forward<U>(u), std::forward<T>(t));};

[ Note

:

The semantics of the swappable and swappable_­withconcepts are fully defined by the ranges​::​swap customization point.

end note

]

[ Example

:

User code can ensure that the evaluation of swap calls is performed in an appropriate context under the various conditions as follows:

#include #include #include

namespace ranges = std::ranges;

template<class T, std::swappable_with U> void value_swap(T&& t, U&& u) { ranges::swap(std::forward(t), std::forward(u)); }

template<std::swappable T> void lv_swap(T& t1, T& t2) { ranges::swap(t1, t2); }

namespace N { struct A { int m; }; struct Proxy { A* a; Proxy(A& a) : a{&a} {} friend void swap(Proxy x, Proxy y) { ranges::swap(*x.a, *y.a); } }; Proxy proxy(A& a) { return Proxy{ a }; } }

int main() { int i = 1, j = 2; lv_swap(i, j); assert(i == 2 && j == 1);

N::A a1 = { 5 }, a2 = { -5 }; value_swap(a1, proxy(a2)); assert(a1.m == -5 && a2.m == 5); }

end example

]