dune-common  2.11
compare.hh
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifndef DUNE_COMMON_STD_COMPARE_HH
6 #define DUNE_COMMON_STD_COMPARE_HH
7 
8 #include <compare>
9 #include <concepts>
10 #include <type_traits>
11 #include <utility>
12 
21 namespace Dune::Std {
22 namespace Impl {
23 
24 template <class T, class Cat>
25 concept comparesAs =
26  std::same_as<std::common_comparison_category_t<T, Cat>, Cat>;
27 
28 template <class T, class U>
29 concept weaklyEqualityComparableWith =
30  requires(const std::remove_reference_t<T>& t,
31  const std::remove_reference_t<U>& u)
32  {
33  { t == u } -> std::convertible_to<bool>;
34  { t != u } -> std::convertible_to<bool>;
35  { u == t } -> std::convertible_to<bool>;
36  { u != t } -> std::convertible_to<bool>;
37  };
38 
39 template <class T, class U>
40 concept partiallyOrderedWith =
41  requires(const std::remove_reference_t<T>& t,
42  const std::remove_reference_t<U>& u)
43  {
44  { t < u } -> std::convertible_to<bool>;
45  { t > u } -> std::convertible_to<bool>;
46  { t <= u } -> std::convertible_to<bool>;
47  { t >= u } -> std::convertible_to<bool>;
48  { u < t } -> std::convertible_to<bool>;
49  { u > t } -> std::convertible_to<bool>;
50  { u <= t } -> std::convertible_to<bool>;
51  { u >= t } -> std::convertible_to<bool>;
52  };
53 
54 template <class T, class U, class C = std::common_reference_t<const T&, const U&>>
55 concept comparisonCommonTypeWithImpl =
56  std::same_as<std::common_reference_t<const T&, const U&>,
57  std::common_reference_t<const U&, const T&>> &&
58  requires
59  {
60  requires std::convertible_to<const T&, const C&> ||
61  std::convertible_to<T, const C&>;
62  requires std::convertible_to<const U&, const C&> ||
63  std::convertible_to<U, const C&>;
64  };
65 
66 template <class T, class U>
67 concept comparisonCommonTypeWith =
68  comparisonCommonTypeWithImpl<std::remove_cvref_t<T>, std::remove_cvref_t<U>>;
69 
70 } // end namespace Impl
71 
79 template <class T, class Cat = std::partial_ordering>
81  Impl::weaklyEqualityComparableWith<T, T> &&
82  Impl::partiallyOrderedWith<T, T> &&
83  requires(const std::remove_reference_t<T>& a,
84  const std::remove_reference_t<T>& b)
85  {
86  { a <=> b } -> Impl::comparesAs<Cat>;
87  };
88 
89 
99 template <class T, class U, class Cat = std::partial_ordering>
101  Std::three_way_comparable<T, Cat> &&
102  Std::three_way_comparable<U, Cat> &&
103  Impl::comparisonCommonTypeWith<T, U> &&
105  std::common_reference_t<
106  const std::remove_reference_t<T>&,
107  const std::remove_reference_t<U>&>, Cat> &&
108  Impl::weaklyEqualityComparableWith<T, U> &&
109  Impl::partiallyOrderedWith<T, U> &&
110  requires(const std::remove_reference_t<T>& t,
111  const std::remove_reference_t<U>& u)
112  {
113  { t <=> u } -> Impl::comparesAs<Cat>;
114  { u <=> t } -> Impl::comparesAs<Cat>;
115  };
116 
119 {
120  template <class T, class U>
121  constexpr auto operator() (T&& t, U&& u) const
122  {
123  return std::forward<T>(t) <=> std::forward<U>(u);
124  }
125 };
126 
127 } // end namespace Dune::Std
128 
129 #endif // DUNE_COMMON_STD_COMPARE_HH
Namespace for features backported from new C++ standards.
Definition: algorithm.hh:19
requires(((std::is_integral_v< I > or Dune::IsIntegralConstant< I >::value) &&...)) HybridMultiIndex(I... i) -> HybridMultiIndex< decltype(Impl::castToHybridSizeT(i))... >
A functor implementing the three-way comparison on the arguments.
Definition: compare.hh:118
constexpr auto operator()(T &&t, U &&u) const
Definition: compare.hh:121
concept three_way_comparable
The concept std::three_way_comparable specifies that the three way comparison operator <=> on T yield...
Definition: compare.hh:80
concept three_way_comparable_with
The concept std::three_way_comparable_with specifies that the three way comparison operator <=> on (p...
Definition: compare.hh:100