Path: blob/main/contrib/llvm-project/libcxx/include/__concepts/swappable.h
35262 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___CONCEPTS_SWAPPABLE_H9#define _LIBCPP___CONCEPTS_SWAPPABLE_H1011#include <__concepts/assignable.h>12#include <__concepts/class_or_enum.h>13#include <__concepts/common_reference_with.h>14#include <__concepts/constructible.h>15#include <__config>16#include <__type_traits/extent.h>17#include <__type_traits/is_nothrow_assignable.h>18#include <__type_traits/is_nothrow_constructible.h>19#include <__type_traits/remove_cvref.h>20#include <__utility/exchange.h>21#include <__utility/forward.h>22#include <__utility/move.h>23#include <__utility/swap.h>24#include <cstddef>2526#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)27# pragma GCC system_header28#endif2930_LIBCPP_PUSH_MACROS31#include <__undef_macros>3233_LIBCPP_BEGIN_NAMESPACE_STD3435#if _LIBCPP_STD_VER >= 203637// [concept.swappable]3839namespace ranges {40namespace __swap {4142template <class _Tp>43void swap(_Tp&, _Tp&) = delete;4445// clang-format off46template <class _Tp, class _Up>47concept __unqualified_swappable_with =48(__class_or_enum<remove_cvref_t<_Tp>> || __class_or_enum<remove_cvref_t<_Up>>) &&49requires(_Tp&& __t, _Up&& __u) {50swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));51};52// clang-format on5354struct __fn;5556// clang-format off57template <class _Tp, class _Up, size_t _Size>58concept __swappable_arrays =59!__unqualified_swappable_with<_Tp (&)[_Size], _Up (&)[_Size]> &&60extent_v<_Tp> == extent_v<_Up> &&61requires(_Tp (&__t)[_Size], _Up (&__u)[_Size], const __fn& __swap) {62__swap(__t[0], __u[0]);63};64// clang-format on6566template <class _Tp>67concept __exchangeable =68!__unqualified_swappable_with<_Tp&, _Tp&> && move_constructible<_Tp> && assignable_from<_Tp&, _Tp>;6970struct __fn {71// 2.1 `S` is `(void)swap(E1, E2)`* if `E1` or `E2` has class or enumeration type and...72// *The name `swap` is used here unqualified.73template <class _Tp, class _Up>74requires __unqualified_swappable_with<_Tp, _Up>75_LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp&& __t, _Up&& __u) const76noexcept(noexcept(swap(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {77swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));78}7980// 2.2 Otherwise, if `E1` and `E2` are lvalues of array types with equal extent and...81template <class _Tp, class _Up, size_t _Size>82requires __swappable_arrays<_Tp, _Up, _Size>83_LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp (&__t)[_Size], _Up (&__u)[_Size]) const84noexcept(noexcept((*this)(*__t, *__u))) {85// TODO(cjdb): replace with `ranges::swap_ranges`.86for (size_t __i = 0; __i < _Size; ++__i) {87(*this)(__t[__i], __u[__i]);88}89}9091// 2.3 Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models...92template <__exchangeable _Tp>93_LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp& __x, _Tp& __y) const94noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_assignable_v<_Tp>) {95__y = std::exchange(__x, std::move(__y));96}97};98} // namespace __swap99100inline namespace __cpo {101inline constexpr auto swap = __swap::__fn{};102} // namespace __cpo103} // namespace ranges104105template <class _Tp>106concept swappable = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };107108template <class _Tp, class _Up>109concept swappable_with = common_reference_with<_Tp, _Up> && requires(_Tp&& __t, _Up&& __u) {110ranges::swap(std::forward<_Tp>(__t), std::forward<_Tp>(__t));111ranges::swap(std::forward<_Up>(__u), std::forward<_Up>(__u));112ranges::swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));113ranges::swap(std::forward<_Up>(__u), std::forward<_Tp>(__t));114};115116#endif // _LIBCPP_STD_VER >= 20117118_LIBCPP_END_NAMESPACE_STD119120_LIBCPP_POP_MACROS121122#endif // _LIBCPP___CONCEPTS_SWAPPABLE_H123124125