DUNE PDELab (unstable)

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
21namespace Dune::Std {
22namespace Impl {
23
24template <class T, class Cat>
25concept comparesAs =
26 std::same_as<std::common_comparison_category_t<T, Cat>, Cat>;
27
28template <class T, class U>
29concept 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
39template <class T, class U>
40concept 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
54template <class T, class U, class C = std::common_reference_t<const T&, const U&>>
55concept 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
66template <class T, class U>
67concept comparisonCommonTypeWith =
68 comparisonCommonTypeWithImpl<std::remove_cvref_t<T>, std::remove_cvref_t<U>>;
69
70} // end namespace Impl
71
79template <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
99template <class T, class U, class Cat = std::partial_ordering>
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
The concept std::three_way_comparable_with specifies that the three way comparison operator <=> on (p...
Definition: compare.hh:100
The concept std::three_way_comparable specifies that the three way comparison operator <=> on T yield...
Definition: compare.hh:80
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:119
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Oct 20, 22:40, 2025)