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 elements 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.

Functionally equivalent to std::swap , including support for c-arrays. Unlike std::swap , arene::base::swap is implemented as a Customization Point Object. This means it 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: