Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
comparison_traits.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "Header defines a group of traits, so the name reflects that"
2// Copyright 2024, Toyota Motor Corporation
3//
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_COMPARISON_TRAITS_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_COMPARISON_TRAITS_HPP_
8
9// IWYU pragma: private, include "arene/base/type_traits.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
14#include "arene/base/stdlib_choice/declval.hpp"
15#include "arene/base/stdlib_choice/enable_if.hpp"
16#include "arene/base/stdlib_choice/is_convertible.hpp"
17
18namespace arene {
19namespace base {
20
21/// Type trait to check if an instance of type @c T can be compared to an
22/// instance of type @c U with the less-than operator.
23/// @tparam T The type of the lhs
24/// @tparam U The type of the rhs. Defaults to @c T
25/// @return @c true if an instance of @c T can be compared with an instance of
26/// @c U, @c false otherwise.
27template <typename T, typename U = T, typename = constraints<>>
28extern constexpr bool is_less_than_comparable_v = false;
29
30/// Specialization for the case that @c T and @c U can be compared.
31template <typename T, typename U>
32extern constexpr bool is_less_than_comparable_v<
33 T,
34 U,
36 true;
37
38/// Type trait to check if an instance of type @c T can be compared to an
39/// instance of type @c U with the less-than operator without throwing an exception.
40/// @tparam T The type of the lhs
41/// @tparam U The type of the rhs. Defaults to @c T
42/// @return @c true if an instance of @c T can be compared with an instance of
43/// @c U without throwing, @c false otherwise.
44template <typename T, typename U = T, typename = constraints<>>
45extern constexpr bool is_nothrow_less_than_comparable_v = false;
46
47/// Specialization for the case that @c T and @c U can be compared.
48template <typename T, typename U>
49extern constexpr bool
51 noexcept(std::declval<T>() < std::declval<U>());
52
53/// Type trait to check if an instance of type @c T can be compared to an
54/// instance of type @c U with the less-than-or-equal operator.
55/// @tparam T The type of the lhs
56/// @tparam U The type of the rhs. Defaults to @c T
57/// @return @c true if an instance of @c T can be compared with an instance of
58/// @c U, @c false otherwise.
59template <typename T, typename U = T, typename = constraints<>>
60extern constexpr bool is_less_than_or_equal_comparable_v = false;
61
62/// Specialization for the case that @c T and @c U can be compared.
63template <typename T, typename U>
64extern constexpr bool is_less_than_or_equal_comparable_v<
65 T,
66 U,
68 true;
69
70/// Type trait to check if an instance of type @c T can be compared to an
71/// instance of type @c U with the less-than-or-equal operator without throwing an exception.
72/// @tparam T The type of the lhs
73/// @tparam U The type of the rhs. Defaults to @c T
74/// @return @c true if an instance of @c T can be compared with an instance of
75/// @c U without throwing, @c false otherwise.
76template <typename T, typename U = T, typename = constraints<>>
77extern constexpr bool is_nothrow_less_than_or_equal_comparable_v = false;
78
79/// Specialization for the case that @c T and @c U can be compared.
80template <typename T, typename U>
82 T,
83 U,
85 noexcept(std::declval<T>() <= std::declval<U>());
86
87/// Type trait to check if an instance of type @c T can be compared to an
88/// instance of type @c U with the greater-than operator.
89/// @tparam T The type of the lhs
90/// @tparam U The type of the rhs. Defaults to @c T
91/// @return @c true if an instance of @c T can be compared with an instance of
92/// @c U, @c false otherwise.
93template <typename T, typename U = T, typename = constraints<>>
94extern constexpr bool is_greater_than_comparable_v = false;
95
96/// Specialization for the case that @c T and @c U can be compared.
97template <typename T, typename U>
98extern constexpr bool is_greater_than_comparable_v<
99 T,
100 U,
102 true;
103
104/// Type trait to check if an instance of type @c T can be compared to an
105/// instance of type @c U with the greater-than operator without throwing an exception.
106/// @tparam T The type of the lhs
107/// @tparam U The type of the rhs. Defaults to @c T
108/// @return @c true if an instance of @c T can be compared with an instance of
109/// @c U without throwing, @c false otherwise.
110template <typename T, typename U = T, typename = constraints<>>
111extern constexpr bool is_nothrow_greater_than_comparable_v = false;
112
113/// Specialization for the case that @c T and @c U can be compared.
114template <typename T, typename U>
115extern constexpr bool
117 noexcept(std::declval<T>() > std::declval<U>());
118
119/// Type trait to check if an instance of type @c T can be compared to an
120/// instance of type @c U with the greater-than-or-equal operator.
121/// @tparam T The type of the lhs
122/// @tparam U The type of the rhs. Defaults to @c T
123/// @return @c true if an instance of @c T can be compared with an instance of
124/// @c U, @c false otherwise.
125template <typename T, typename U = T, typename = constraints<>>
126extern constexpr bool is_greater_than_or_equal_comparable_v = false;
127
128/// Specialization for the case that @c T and @c U can be compared.
129template <typename T, typename U>
130extern constexpr bool is_greater_than_or_equal_comparable_v<
131 T,
132 U,
134 true;
135
136/// Type trait to check if an instance of type @c T can be compared to an
137/// instance of type @c U with the greater-than-or-equal operator without throwing an exception.
138/// @tparam T The type of the lhs
139/// @tparam U The type of the rhs. Defaults to @c T
140/// @return @c true if an instance of @c T can be compared with an instance of
141/// @c U without throwing, @c false otherwise.
142template <typename T, typename U = T, typename = constraints<>>
143extern constexpr bool is_nothrow_greater_than_or_equal_comparable_v = false;
144
145/// Specialization for the case that @c T and @c U can be compared.
146template <typename T, typename U>
148 T,
149 U,
151 noexcept(std::declval<T>() >= std::declval<U>());
152
153namespace comparison_traits_detail {
154/// @brief Helper class for @c is_equality_comparable_v
155class is_equality_comparable_helper {
156 public:
158 ARENE_IGNORE_ALL("-Wfloat-equal", "Checking for existence of comparison, not using it");
159 /// @brief Helper function for checking if two types are equality comparable
160 /// @tparam Lhs The type of the left hand operand
161 /// @tparam Rhs The type of the right hand operand
162 /// @tparam Arg The deduced type of the argument
163 /// @return Always returns @c true ; this overload is only selected if the types are comparable
164 template <typename Lhs, typename Rhs, typename Arg, typename = decltype(std::declval<Lhs>() == std::declval<Rhs>())>
165 static constexpr auto test(Arg) noexcept -> bool {
166 return true;
167 }
169
170 /// @brief Helper function for checking if two types are equality comparable
171 /// @tparam Lhs The type of the left hand operand
172 /// @tparam Rhs The type of the right hand operand
173 /// @return Always returns @c false ; this overload is only selected if the types are not comparable
174 template <typename Lhs, typename Rhs>
175 static constexpr auto test(void*) noexcept -> bool {
176 return false;
177 }
178};
179} // namespace comparison_traits_detail
180
181/// Type trait to check if an instance of type @c Lhs can be compared to an
182/// instance of type @c Rhs with the equality operator.
183/// @tparam Lhs The type of the lhs
184/// @tparam Rhs The type of the rhs. Defaults to @c Lhs
185/// @return @c true if an instance of @c Lhs can be compared with an instance of
186/// @c Rhs, @c false otherwise.
187template <typename Lhs, typename Rhs = Lhs>
188extern constexpr bool is_equality_comparable_v{
189 //
190 comparison_traits_detail::is_equality_comparable_helper::test<Lhs, Rhs>(0)
191};
192
193/// Type trait to check if an instance of type @c T can be compared to an
194/// instance of type @c U with the equality operator without throwing an exception.
195/// @tparam T The type of the lhs
196/// @tparam U The type of the rhs. Defaults to @c T
197/// @return @c true if an instance of @c T can be compared with an instance of
198/// @c U without throwing, @c false otherwise.
199template <typename T, typename U = T, typename = constraints<>>
200extern constexpr bool is_nothrow_equality_comparable_v = false;
201
203ARENE_IGNORE_ALL("-Wfloat-equal", "Checking for existence of comparison, not using it");
204/// Specialization for the case that @c T and @c U can be compared.
205template <typename T, typename U>
206extern constexpr bool
208 noexcept(std::declval<T>() == std::declval<U>());
210
211/// Type trait to check if an instance of type @c T can be compared to an
212/// instance of type @c U with the inequality operator.
213/// @tparam T The type of the lhs
214/// @tparam U The type of the rhs. Defaults to @c T
215/// @return @c true if an instance of @c T can be compared with an instance of
216/// @c U, @c false otherwise.
217template <typename T, typename U = T, typename = constraints<>>
218extern constexpr bool is_inequality_comparable_v = false;
219
220/// Specialization for the case that @c T and @c U can be compared.
221template <typename T, typename U>
222extern constexpr bool is_inequality_comparable_v<
223 T,
224 U,
226 true;
227
228/// Type trait to check if an instance of type @c T can be compared to an
229/// instance of type @c U with the inequality operator without throwing an exception.
230/// @tparam T The type of the lhs
231/// @tparam U The type of the rhs. Defaults to @c T
232/// @return @c true if an instance of @c T can be compared with an instance of
233/// @c U without throwing, @c false otherwise.
234template <typename T, typename U = T, typename = constraints<>>
235extern constexpr bool is_nothrow_inequality_comparable_v = false;
236
237/// Specialization for the case that @c T and @c U can be compared.
238template <typename T, typename U>
239extern constexpr bool
241 noexcept(std::declval<T>() != std::declval<U>());
242
243} // namespace base
244} // namespace arene
245
246#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_COMPARISON_TRAITS_HPP_
Provides macros used to disable and enable compiler specific diagnostics using the _Pragma operator.
#define ARENE_IGNORE_END()
Ends a region of a suppressed diagnostic.
Definition diagnostics.hpp:136
#define ARENE_IGNORE_START()
Begins a region of a suppressed diagnostic.
Definition diagnostics.hpp:131
#define ARENE_IGNORE_ALL(s, comment)
Pushes an ignore for a diagnostic into the compiler for any compiler.
Definition diagnostics.hpp:122
Definition accumulate.hpp:37
constexpr bool is_greater_than_or_equal_comparable_v
constexpr bool is_nothrow_less_than_or_equal_comparable_v
constexpr bool is_nothrow_greater_than_or_equal_comparable_v
constexpr bool is_inequality_comparable_v
constexpr bool is_less_than_comparable_v
constexpr bool is_nothrow_greater_than_comparable_v
constexpr bool is_less_than_or_equal_comparable_v
constexpr bool is_nothrow_equality_comparable_v
constexpr bool is_nothrow_inequality_comparable_v
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
constexpr bool is_greater_than_comparable_v
constexpr bool is_nothrow_less_than_comparable_v
constexpr bool is_equality_comparable_v
Definition accumulate.hpp:36
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...