Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
arene::base::swap_cpo Namespace Reference

Variable Documentation

◆ swap

ARENE_MAYBE_UNUSED constexpr auto const& arene::base::swap_cpo::swap
staticconstexpr
Initial value:
=
::arene::base::detail::cpp14_inline_static_const<swap_detail::swap_fn_impl>::value

A Customization Point Object implementation of the constexpr compatible std::swap from C++17.

Template Parameters
Ttype of the first element to swap.
Utype of the second element to swap.
Parameters
lhselement(s) to swap.
rhselement(s) to swap.
Postcondition
lhs and rhs will have had their states exchanged by one of the described methods.

Occupies a position between std::swap and std::ranges::swap. Like the latter, this is a Customization Point Object (CPO) that picks up user-specified swap overloads using ADL, and can swap pairs of arguments which are not the same type when a custom ADL implementation exists for them. Unlike std::ranges::swap (but like std::swap), this function object is not invocable on rvalue operands.

As a CPO, arene::base::swap has the following properties:

  1. It supports user customizations directly, performing discovery of swap customizations automatically. Customizations are given the following preference:
    1. ADL-discovered swap(lhs,rhs) , if it exists.
    2. The default swap implementation, equivalent to std::swap .
  2. It is a function object, so it can be used directly in APIs which consume functors without the need of a wrapper.
  3. It will never be discoverable via ADL. This avoids ambiguous overload resolution problems with types in arene::base which provide customizations of swap .

This allow simplification of user code. When using std::swap in a generic context, best practice requires the following pattern in order to allow ADL discovery of swap customizations while falling back to the generic std::swap implementation for the default case:

using std::swap;
swap(a, b);

This is easy to forget. With arene::base::swap , this becomes simply: