Dune Core Modules (unstable)

compare.hh
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
19namespace Dune::Std {
20namespace Impl {
21
22template <class T, class Cat>
23concept comparesAs =
24 std::same_as<std::common_comparison_category_t<T, Cat>, Cat>;
25
26template <class T, class U>
27concept weaklyEqualityComparableWith =
28 requires(const std::remove_reference_t<T>& t,
29 const std::remove_reference_t<U>& u)
30 {
31 { t == u } -> std::convertible_to<bool>;
32 { t != u } -> std::convertible_to<bool>;
33 { u == t } -> std::convertible_to<bool>;
34 { u != t } -> std::convertible_to<bool>;
35 };
36
37template <class T, class U>
38concept partiallyOrderedWith =
39 requires(const std::remove_reference_t<T>& t,
40 const std::remove_reference_t<U>& u)
41 {
42 { t < u } -> std::convertible_to<bool>;
43 { t > u } -> std::convertible_to<bool>;
44 { t <= u } -> std::convertible_to<bool>;
45 { t >= u } -> std::convertible_to<bool>;
46 { u < t } -> std::convertible_to<bool>;
47 { u > t } -> std::convertible_to<bool>;
48 { u <= t } -> std::convertible_to<bool>;
49 { u >= t } -> std::convertible_to<bool>;
50 };
51
52template <class T, class U, class C = std::common_reference_t<const T&, const U&>>
53concept comparisonCommonTypeWithImpl =
54 std::same_as<std::common_reference_t<const T&, const U&>,
55 std::common_reference_t<const U&, const T&>> &&
56 requires
57 {
58 requires std::convertible_to<const T&, const C&> ||
59 std::convertible_to<T, const C&>;
60 requires std::convertible_to<const U&, const C&> ||
61 std::convertible_to<U, const C&>;
62 };
63
64template <class T, class U>
65concept comparisonCommonTypeWith =
66 comparisonCommonTypeWithImpl<std::remove_cvref_t<T>, std::remove_cvref_t<U>>;
67
68} // end namespace Impl
69
77template <class T, class Cat = std::partial_ordering>
79 Impl::weaklyEqualityComparableWith<T, T> &&
80 Impl::partiallyOrderedWith<T, T> &&
81 requires(const std::remove_reference_t<T>& a,
82 const std::remove_reference_t<T>& b)
83 {
84 { a <=> b } -> Impl::comparesAs<Cat>;
85 };
86
87
97template <class T, class U, class Cat = std::partial_ordering>
101 Impl::comparisonCommonTypeWith<T, U> &&
103 std::common_reference_t<
104 const std::remove_reference_t<T>&,
105 const std::remove_reference_t<U>&>, Cat> &&
106 Impl::weaklyEqualityComparableWith<T, U> &&
107 Impl::partiallyOrderedWith<T, U> &&
108 requires(const std::remove_reference_t<T>& t,
109 const std::remove_reference_t<U>& u)
110 {
111 { t <=> u } -> Impl::comparesAs<Cat>;
112 { u <=> t } -> Impl::comparesAs<Cat>;
113 };
114
117{
118 template <class T, class U>
119 constexpr auto operator() (T&& t, U&& u) const
120 {
121 return std::forward<T>(t) <=> std::forward<U>(u);
122 }
123};
124
125} // end namespace Dune::Std
126
127#endif // DUNE_COMMON_STD_COMPARE_HH
The concept std::three_way_comparable_with specifies that the three way comparison operator <=> on (p...
Definition: compare.hh:98
The concept std::three_way_comparable specifies that the three way comparison operator <=> on T yield...
Definition: compare.hh:78
Namespace for features backported from new C++ standards.
Definition: algorithm.hh:19
A functor implementing the three-way comparison on the arguments.
Definition: compare.hh:117
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jan 8, 23:30, 2025)