Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
operators.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPARE_OPERATORS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPARE_OPERATORS_HPP_
7
8// IWYU pragma: private, include "arene/base/compare.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
20#include "arene/base/stdlib_choice/declval.hpp"
21#include "arene/base/stdlib_choice/enable_if.hpp"
22#include "arene/base/stdlib_choice/is_base_of.hpp"
23#include "arene/base/stdlib_choice/is_class.hpp"
24#include "arene/base/stdlib_choice/is_same.hpp"
26// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
27
28// parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: It is allowed to declare comparison operators as friend
29// functions"
30// parasoft-begin-suppress AUTOSAR-A13_5_5-b-2 "Mixed comparisons permitted by A13-5-5 Permit #1"
31
32namespace arene {
33namespace base {
34
35namespace operators_detail {
36/// @brief Helper class to define operator != in terms of the == operator with a left-hand operand of type @c LhsType
37/// and a right-hand operand of type @c RhsType
38/// @tparam LhsType The type of the left-hand operator parameter
39/// @tparam RhsType The type of the right-hand operator parameter
40template <typename LhsType, typename RhsType>
41class inequality_operator_mixin {
42 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
43 /// @brief Compare whether two objects are not equal.
44 ///
45 /// @param lhs Left-hand side of != operator.
46 /// @param rhs Right-hand side of != operator.
47 /// @return Return @c true if @c lhs is not equal to @c rhs, otherwise @c
48 /// false.
49 ARENE_NODISCARD friend constexpr auto operator!=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
50 static_assert(is_nothrow_equality_comparable_v<LhsType const&, RhsType const&>, "operator == should be noexcept");
51 return !(lhs == rhs);
52 }
53 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
54
55 protected:
56 /// @brief Default constructor is protected
57 constexpr inequality_operator_mixin() noexcept = default;
58 /// @brief Default destructor is protected
59 ~inequality_operator_mixin() noexcept = default;
60 /// @brief Default copy constructor is protected
61 /// @param other The value to copy from
62 constexpr inequality_operator_mixin(inequality_operator_mixin const& other) noexcept = default;
63 /// @brief Default move constructor is protected
64 /// @param other The value to copy from
65 constexpr inequality_operator_mixin(inequality_operator_mixin&& other) noexcept = default;
66 /// @brief Default copy assignment is protected
67 /// @param other The value to copy from
68 constexpr auto operator=(inequality_operator_mixin const& other) & noexcept -> inequality_operator_mixin& = default;
69 /// @brief Default move assignment is protected
70 /// @param other The value to copy from
71 constexpr auto operator=(inequality_operator_mixin&& other) & noexcept -> inequality_operator_mixin& = default;
72};
73
74} // namespace operators_detail
75
76/// @brief Helper class that can be inherited from by a class to define the != operator in terms of the == operator
77/// defined by that class.
78///
79/// Requires that operator==(const Derived&, const Other&) is defined and does not throw exceptions
80///
81/// @tparam Derived The type of the derived class. The comparison operators provided take parameters of this type.
82/// @tparam Other The type of the other object to compare. The comparison operators provided take parameters of this
83/// type.
84///
85/// For example:
86/// @code {cpp}
87/// class MyClass : arene::base::full_equality_operators_from_basic_equality<MyClass>
88/// {
89/// ...
90/// // No need to explicitly define 'operator!=' here.
91/// friend bool operator==(const MyClass& a, const MyClass& b) noexcept;
92/// };
93/// @endcode
94// parasoft-begin-suppress AUTOSAR-A10_1_1-a-2 "False positive: all base classes are empty"
95template <typename Derived, typename Other = Derived>
97 : operators_detail::inequality_operator_mixin<Derived, Other>
98 , operators_detail::inequality_operator_mixin<Other, Derived> {
99 // parasoft-end-suppress
100 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
101 /// @brief Equality comparison operator for instances of @c Derived and @c Other.
102 /// @param lhs The first object being compared
103 /// @param rhs The second object being compared
104 /// @return bool Result of @c lhs==rhs
105 ARENE_NODISCARD friend constexpr auto operator==(Other const& lhs, Derived const& rhs) noexcept -> bool {
106 return rhs == lhs;
107 }
108 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
109
110 protected:
111 /// @brief Default constructor is protected
112 constexpr full_equality_operators_from_basic_equality() noexcept = default;
113 /// @brief Default destructor is protected
115 /// @brief Default copy constructor is protected
116 /// @param other The value to copy from
118 ) noexcept = default;
119 /// @brief Default move constructor is protected
120 /// @param other The value to copy from
122 ) noexcept = default;
123 /// @brief Default copy assignment is protected
124 /// @param other The value to copy from
125 constexpr auto operator=(full_equality_operators_from_basic_equality const& other) & noexcept
127 /// @brief Default move assignment is protected
128 /// @param other The value to copy from
129 constexpr auto operator=(full_equality_operators_from_basic_equality&& other) & noexcept
131};
132
133// @cond INTERNAL
134/// @brief Specialization for when @c Other is @c Derived.
135template <typename Derived>
137 : operators_detail::inequality_operator_mixin<Derived, Derived> {
138 protected:
139 /// @brief Default constructor is protected
140 constexpr full_equality_operators_from_basic_equality() noexcept = default;
141 /// @brief Default destructor is protected
143 /// @brief Default copy constructor is protected
144 /// @param other The value to copy from
146 ) noexcept = default;
147 /// @brief Default move constructor is protected
148 /// @param other The value to copy from
150 ) noexcept = default;
151 /// @brief Default copy assignment is protected
152 /// @param other The value to copy from
153 constexpr auto operator=(full_equality_operators_from_basic_equality const& other) & noexcept
155 /// @brief Default move assignment is protected
156 /// @param other The value to copy from
157 constexpr auto operator=(full_equality_operators_from_basic_equality&& other) & noexcept
159};
160// @endcond
161
162namespace operators_detail {
163/// @brief Helper class to define <, >, <= and >= operators in terms of the @c LhsType::three_way_compare function with
164/// a left-hand operand of type @c LhsType and a right-hand operand of type @c RhsType
165/// @tparam LhsType The type of the left-hand operator parameter
166/// @tparam RhsType The type of the right-hand operator parameter
167template <typename LhsType, typename RhsType>
168class ordering_operators_from_three_way_mixin {
169 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
170 /// @brief Less-than comparison operator for instances of @c LhsType and @c RhsType.
171 /// @param lhs The first object being compared
172 /// @param rhs The second object being compared
173 /// @return @c True if @c lhs is less than @c rhs, @c false otherwise.
174 ARENE_NODISCARD friend constexpr auto operator<(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
175 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
176 // side-effects"
177 static_assert(
178 std::is_same<decltype(LhsType::three_way_compare(lhs, rhs)), strong_ordering>::value,
179 "LhsType class must provide three_way_compare function that "
180 "returns strong_ordering"
181 );
182 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
183 // parasoft-end-suppress CERT_C-PRE31-c-3
184 return LhsType::three_way_compare(lhs, rhs) == strong_ordering::less;
185 }
186 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
187
188 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
189 /// @brief Greater-than comparison operator for instances of @c LhsType and @c RhsType
190 /// @param lhs The first object being compared
191 /// @param rhs The second object being compared
192 /// @return @c True if @c lhs is greater than @c rhs, @c false otherwise.
193 ARENE_NODISCARD friend constexpr auto operator>(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
194 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
195 // side-effects"
196 static_assert(
197 std::is_same<decltype(LhsType::three_way_compare(lhs, rhs)), strong_ordering>::value,
198 "LhsType class must provide three_way_compare function that "
199 "returns strong_ordering"
200 );
201 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
202 // parasoft-end-suppress CERT_C-PRE31-c-3
203 return LhsType::three_way_compare(lhs, rhs) == strong_ordering::greater;
204 }
205 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
206
207 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
208 /// @brief Less-than-or-equal comparison operator for instances of @c LhsType and @c RhsType.
209 /// @param lhs The first object being compared
210 /// @param rhs The second object being compared
211 /// @return @c True if @c lhs is less than or equal to @c rhs, @c false
212 /// otherwise.
213 ARENE_NODISCARD friend constexpr auto operator<=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
214 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
215 // side-effects"
216 static_assert(
217 std::is_same<decltype(LhsType::three_way_compare(lhs, rhs)), strong_ordering>::value,
218 "LhsType class must provide three_way_compare function that "
219 "returns strong_ordering"
220 );
221 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
222 // parasoft-end-suppress CERT_C-PRE31-c-3
223 auto const cmp_res = LhsType::three_way_compare(lhs, rhs);
224 return cmp_res == strong_ordering::less || cmp_res == strong_ordering::equal;
225 }
226 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
227
228 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
229 /// @brief Greater-than-or-equal comparison operator for instances of @c LhsType and
230 /// @c RhsType.
231 /// @param lhs The first object being compared
232 /// @param rhs The second object being compared
233 /// @return true if @c LhsType::three_way_compare(lhs,rhs) returns @c strong_ordering::greater or @c
234 /// strong_ordering::equal .
235 /// @return false if @c LhsType::three_way_compare(lhs,rhs) returns @c strong_ordering::less .
236 /// otherwise.
237 ARENE_NODISCARD friend constexpr auto operator>=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
238 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
239 // side-effects"
240 static_assert(
241 std::is_same<decltype(LhsType::three_way_compare(lhs, rhs)), strong_ordering>::value,
242 "LhsType class must provide three_way_compare function that "
243 "returns strong_ordering"
244 );
245 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
246 // parasoft-end-suppress CERT_C-PRE31-c-3
247 auto const cmp_res = LhsType::three_way_compare(lhs, rhs);
248 return cmp_res == strong_ordering::greater || cmp_res == strong_ordering::equal;
249 }
250 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
251
252 protected:
253 /// @brief Default constructor is protected
254 constexpr ordering_operators_from_three_way_mixin() noexcept = default;
255 /// @brief Default destructor is protected
256 ~ordering_operators_from_three_way_mixin() noexcept = default;
257 /// @brief Default copy constructor is protected
258 /// @param other The source object
259 constexpr ordering_operators_from_three_way_mixin(ordering_operators_from_three_way_mixin const& other
260 ) noexcept = default;
261 /// @brief Default move constructor is protected
262 /// @param other The source object
263 constexpr ordering_operators_from_three_way_mixin(ordering_operators_from_three_way_mixin&& other) noexcept = default;
264 /// @brief Default copy assignment is protected
265 /// @param other The source object
266 constexpr auto operator=(ordering_operators_from_three_way_mixin const& other) & noexcept
267 -> ordering_operators_from_three_way_mixin& = default;
268 /// @brief Default move assignment is protected
269 /// @param other The source object
270 constexpr auto operator=(ordering_operators_from_three_way_mixin&& other) & noexcept
271 -> ordering_operators_from_three_way_mixin& = default;
272};
273} // namespace operators_detail
274
275/// @brief Helper class that can be inherited from by a class to define the @c <, @c >, @c <= and @c >= comparison
276/// operators in terms of a @c three_way_compare function provided by the Derived class, and define the @c != operator
277/// in terms of the @c == operator.
278///
279/// Requires that the derived class provides a @c static member function @c three_way_compare that accepts a <c>const
280/// Derived&</c> and a <c>const Other&</c>, and returns a @c strong_ordering value indicating whether the first is less
281/// than, equal to or greater than the second. This @c three_way_compare functions must not throw exceptions.
282///
283/// Requires that operator==(const Derived&, const Other&) is defined and does not throw exceptions
284///
285/// @tparam Derived The type of the derived class. The comparison operators provided take parameters of this type.
286/// @tparam Other The type of the other object to compare. The comparison operators provided take parameters of this
287/// type.
288///
289/// For example:
290/// @code {cpp}
291/// class MyClass :
292/// arene::base::full_ordering_operators_from_three_way_compare_and_equals<MyClass> {
293/// ...
294/// friend arene::base::string_ordering three_way_compare(
295/// const MyClass& a, const MyClass& b) noexcept;
296/// friend bool operator==(const MyClass& a, const MyClass& b) noexcept;
297/// };
298/// @endcode
299///
300/// @deprecated Prefer arene::base::generic_ordering_from_three_way_compare
301// parasoft-begin-suppress AUTOSAR-A10_1_1-a-2 "False positive: all base classes are empty"
302template <typename Derived, typename Other = Derived>
303class ARENE_DEPRECATED_WITH("Use generic_ordering_from_three_way_compare instead"
306 , operators_detail::ordering_operators_from_three_way_mixin<Derived, Other> {
307 // parasoft-end-suppress
308 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
309 /// @brief Less-than comparison operator for instances of @c Derived and @c Other.
310 /// @param lhs The first object being compared
311 /// @param rhs The second object being compared
312 /// @return bool The result of @c rhs>lhs .
313 ARENE_NODISCARD friend constexpr auto operator<(Other const& lhs, Derived const& rhs) noexcept -> bool {
314 return rhs > lhs;
315 }
316 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
317
318 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
319 /// @brief Greater-than comparison operator for instances of @c Derived and @c Other.
320 /// @param lhs The first object being compared
321 /// @param rhs The second object being compared
322 /// @return bool The result of @c rhs<lhs .
323 ARENE_NODISCARD friend constexpr auto operator>(Other const& lhs, Derived const& rhs) noexcept -> bool {
324 return rhs < lhs;
325 }
326 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
327
328 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
329 /// @brief Less-than-or-equal comparison operator for instances of @c Derived and @c
330 /// Other.
331 /// @param lhs The first object being compared
332 /// @param rhs The second object being compared
333 /// @return bool The result of @c rhs>=lhs .
334 ARENE_NODISCARD friend constexpr auto operator<=(Other const& lhs, Derived const& rhs) noexcept -> bool {
335 return rhs >= lhs;
336 }
337 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
338
339 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
340 /// @brief Greater-than-or-equal comparison operator for instances of @c Derived and
341 /// @c Other.
342 /// @param lhs The first object being compared
343 /// @param rhs The second object being compared
344 /// @return bool The result of @c rhs<=lhs .
345 ARENE_NODISCARD friend constexpr auto operator>=(Other const& lhs, Derived const& rhs) noexcept -> bool {
346 return rhs <= lhs;
347 }
348 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
349
350 protected:
351 /// @brief Default constructor is protected
353 /// @brief Default destructor is protected
355 /// @brief Default copy constructor is protected
356 /// @param other The source object
359 ) noexcept = default;
360 /// @brief Default move constructor is protected
361 /// @param other The source object
364 ) noexcept = default;
365 /// @brief Default copy assignment is protected
366 /// @param other The source object
367 constexpr auto operator=(full_ordering_operators_from_three_way_compare_and_equals const& other) & noexcept
369 /// @brief Default move assignment is protected
370 /// @param other The source object
371 constexpr auto operator=(full_ordering_operators_from_three_way_compare_and_equals&& other) & noexcept
373};
374
375// @cond INTERNAL
377ARENE_IGNORE_ALL("-Wdeprecated-declarations", "We still want to specialize the deprecated classes");
378/// @brief Specialization for the case that @c Other is @c Derived
379// parasoft-begin-suppress AUTOSAR-A10_1_1-a-2 "False positive: all base classes are empty"
380template <typename Derived>
383 , operators_detail::ordering_operators_from_three_way_mixin<Derived, Derived> {
384 // parasoft-end-suppress
385 protected:
386 /// @brief Default constructor is protected
388 /// @brief Default destructor is protected
390 /// @brief Default copy constructor is protected
391 /// @param other The source object
394 ) noexcept = default;
395 /// @brief Default move constructor is protected
396 /// @param other The source object
399 ) noexcept = default;
400 /// @brief Default copy assignment is protected
401 /// @param other The source object
402 constexpr auto operator=(full_ordering_operators_from_three_way_compare_and_equals const& other) & noexcept
404 /// @brief Default move assignment is protected
405 /// @param other The source object
408};
410// @endcond
411
414 "-Wdeprecated-declarations",
415 "We still want to use the deprecated class in this other deprecated class"
416);
417/// @brief Helper class that can be inherited from by a class to define the @c
418/// <, @c >, @c <=, @c >=, @c ==, and @c != comparison operators in terms of a
419/// @c three_way_compare function provided by the Derived class.
420///
421/// Requires that the derived class provides a @c static member function @c three_way_compare that accepts two <c>const
422/// Derived&</c> and returns a @c strong_ordering value indicating whether the first is less than, equal to or greater
423/// than the second. This @c three_way_compare functions must not throw exceptions.
424///
425/// @tparam Derived The type of the derived class. The comparison operators
426/// provided take parameters of this type.
427///
428/// For example:
429/// @code {cpp}
430/// class MyClass :
431/// arene::base::full_ordering_operators_from_three_way_compare<MyClass> {
432/// ...
433/// friend arene::base::string_ordering three_way_compare(
434/// const MyClass& a, const MyClass& b) noexcept;
435/// };
436/// @endcode
437///
438/// @deprecated Prefer arene::base::generic_ordering_from_three_way_compare
439template <typename Derived, typename Other = Derived>
440class ARENE_DEPRECATED_WITH("Use generic_ordering_from_three_way_compare instead"
443 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
444 /// @brief Equality comparison operator for instances of @c Derived and @c Other.
445 /// @param lhs The first object being compared
446 /// @param rhs The second object being compared
447 /// @return @c True if @c lhs is equal to @c rhs, @c false otherwise.
448 ARENE_NODISCARD friend constexpr auto operator==(Derived const& lhs, Other const& rhs) noexcept -> bool {
449 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
450 // side-effects"
451 static_assert(
452 std::is_same<decltype(Derived::three_way_compare(lhs, rhs)), strong_ordering>::value,
453 "Derived class must provide three_way_compare function that "
454 "returns strong_ordering"
455 );
456 static_assert(noexcept(Derived::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
457 // parasoft-end-suppress CERT_C-PRE31-c-3
458 return Derived::three_way_compare(lhs, rhs) == strong_ordering::equal;
459 }
460 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
461
462 protected:
463 /// @brief Default constructor is protected
465 /// @brief Default destructor is protected
467 /// @brief Default copy constructor is protected
468 /// @param other The source object
470 ) noexcept = default;
471 /// @brief Default move constructor is protected
472 /// @param other The source object
474 ) noexcept = default;
475 /// @brief Default copy assignment is protected
476 /// @param other The source object
477 constexpr auto operator=(full_ordering_operators_from_three_way_compare const& other) & noexcept
479 /// @brief Default move assignment is protected
480 /// @param other The source object
481 constexpr auto operator=(full_ordering_operators_from_three_way_compare&& other) & noexcept
483};
485
486namespace operators_detail {
487/// @brief Helper class to define >, <= and >= operators in terms of the < operator with a left-hand operand of type @c
488/// LhsType and a right-hand operand of type @c RhsType
489/// @tparam LhsType The type of the left-hand operator parameter
490/// @tparam RhsType The type of the right-hand operator parameter
491template <typename LhsType, typename RhsType>
492class ordering_operators_from_less_than_mixin {
493 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
494 /// @brief Greater-than comparison operator for instances of @c LhsType and @c RhsType
495 /// @param lhs The first object being compared
496 /// @param rhs The second object being compared
497 /// @return @c True if @c lhs is greater than @c rhs, @c false otherwise.
498 ARENE_NODISCARD friend constexpr auto operator>(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
499 static_assert(is_nothrow_less_than_comparable_v<RhsType const&, LhsType const&>, "rhs < lhs should be noexcept");
500 return static_cast<bool>(rhs < lhs);
501 }
502 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
503
504 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
505 /// @brief Less-than-or-equal comparison operator for instances of @c LhsType and @c
506 /// RhsType.
507 /// @param lhs The first object being compared
508 /// @param rhs The second object being compared
509 /// @return @c True if @c lhs is less than or equal to @c rhs, @c false
510 /// otherwise.
511 ARENE_NODISCARD friend constexpr auto operator<=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
512 static_assert(is_nothrow_less_than_comparable_v<RhsType const&, LhsType const&>, "rhs < lhs should be noexcept");
513 return !static_cast<bool>(rhs < lhs);
514 }
515 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
516
517 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
518 /// @brief Greater-than-or-equal comparison operator for instances of @c LhsType and
519 /// @c RhsType.
520 /// @param lhs The first object being compared
521 /// @param rhs The second object being compared
522 /// @return @c True if @c lhs is greater than or equal to @c rhs, @c false
523 /// otherwise.
524 ARENE_NODISCARD friend constexpr auto operator>=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
525 static_assert(is_nothrow_less_than_comparable_v<RhsType const&, LhsType const&>, "rhs < lhs should be noexcept");
526 return !static_cast<bool>(lhs < rhs);
527 }
528 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
529
530 protected:
531 /// @brief Default constructor is protected
532 constexpr ordering_operators_from_less_than_mixin() noexcept = default;
533 /// @brief Default destructor is protected
534 ~ordering_operators_from_less_than_mixin() noexcept = default;
535 /// @brief Default copy constructor is protected
536 /// @param other The source object
537 constexpr ordering_operators_from_less_than_mixin(ordering_operators_from_less_than_mixin const& other
538 ) noexcept = default;
539 /// @brief Default move constructor is protected
540 /// @param other The source object
541 constexpr ordering_operators_from_less_than_mixin(ordering_operators_from_less_than_mixin&& other) noexcept = default;
542 /// @brief Default copy assignment is protected
543 /// @param other The source object
544 constexpr auto operator=(ordering_operators_from_less_than_mixin const& other) & noexcept
545 -> ordering_operators_from_less_than_mixin& = default;
546 /// @brief Default move assignment is protected
547 /// @param other The source object
548 constexpr auto operator=(ordering_operators_from_less_than_mixin&& other) & noexcept
549 -> ordering_operators_from_less_than_mixin& = default;
550};
551} // namespace operators_detail
552
553/// @brief Helper class that can be inherited from by a class to define the >, <=, >=, != operators in terms of a <
554/// operator and == operator that the derived class provides.
555///
556/// The derived class is required to provide:
557/// - operator<(const Derived&, const Other&)
558/// - operator<(const Other&, const Derived&)
559/// - operator==(const Derived&, const Other&)
560///
561/// This class defines the following operators in terms of those:
562/// - operator>(const Derived&, const Other&)
563/// - operator<=(const Derived&, const Other&)
564/// - operator>=(const Derived&, const Other&)
565/// - operator!=(const Derived&, const Other&)
566/// - operator>(const Other&, const Derived&)
567/// - operator<=(const Other&, const Derived&)
568/// - operator>=(const Other&, const Derived&)
569/// - operator==(const Other&, const Derived&)
570/// - operator!=(const Other&, const Derived&)
571///
572/// For example:
573/// @code {cpp}
574/// class MyString :
575/// arene::base::full_ordering_operators_from_less_than_and_equals<MyString, const
576/// char*> {
577/// ...
578/// // Only need to define less-than and equals. Others defined in terms of
579/// this.
580/// friend bool operator<(const MyString& a, const char* b) noexcept;
581/// friend bool operator<(const char* a, const MyString& b) noexcept;
582/// friend bool operator==(const MyString& a, const char* b) noexcept;
583/// };
584/// @endcode
585// parasoft-begin-suppress AUTOSAR-A10_1_1-a-2 "False positive: all base classes are empty"
586template <typename Derived, typename Other = Derived>
588 : operators_detail::ordering_operators_from_less_than_mixin<Derived, Other>
589 , operators_detail::ordering_operators_from_less_than_mixin<Other, Derived>
591 // parasoft-end-suppress
592 protected:
593 /// @brief Default constructor is protected
595 /// @brief Default destructor is protected
597 /// @brief Default copy constructor is protected
598 /// @param other The source object
601 ) noexcept = default;
602 /// @brief Default move constructor is protected
603 /// @param other The source object
605 ) noexcept = default;
606 /// @brief Default copy assignment is protected
607 /// @param other The source object
608 constexpr auto operator=(full_ordering_operators_from_less_than_and_equals const& other) & noexcept
610 /// @brief Default move assignment is protected
611 /// @param other The source object
612 constexpr auto operator=(full_ordering_operators_from_less_than_and_equals&& other) & noexcept
614};
615
616// @cond INTERNAL
617/// @brief Specialization for the case that @c Other is @c Derived
618template <typename Derived>
620 : operators_detail::ordering_operators_from_less_than_mixin<Derived, Derived>
622 protected:
623 /// @brief Default constructor is protected
624 constexpr full_ordering_operators_from_less_than_and_equals() noexcept = default;
625 /// @brief Default destructor is protected
627 /// @brief Default copy constructor is protected
628 /// @param other The source object
631 ) noexcept = default;
632 /// @brief Default move constructor is protected
633 /// @param other The source object
635 ) noexcept = default;
636 /// @brief Default copy assignment is protected
637 /// @param other The source object
638 constexpr auto operator=(full_ordering_operators_from_less_than_and_equals const& other) & noexcept
640 /// @brief Default move assignment is protected
641 /// @param other The source object
642 constexpr auto operator=(full_ordering_operators_from_less_than_and_equals&& other) & noexcept
644};
645// @endcond
646
647/// @brief Helper class that can be inherited from by a class to define the >, <=, >=, == and != operators in terms of a
648/// < operator that the derived class provides.
649///
650/// The derived class is required to provide:
651/// - operator<(const Derived&, const Other&)
652/// - operator<(const Other&, const Derived&)
653///
654/// This class defines the following operators in terms of those:
655/// - operator>(const Derived&, const Other&)
656/// - operator<=(const Derived&, const Other&)
657/// - operator>=(const Derived&, const Other&)
658/// - operator==(const Derived&, const Other&)
659/// - operator!=(const Derived&, const Other&)
660/// - operator>(const Other&, const Derived&)
661/// - operator<=(const Other&, const Derived&)
662/// - operator>=(const Other&, const Derived&)
663/// - operator==(const Other&, const Derived&)
664/// - operator!=(const Other&, const Derived&)
665///
666/// For example:
667/// @code {cpp}
668/// class MyString : arene::base::full_ordering_operators_from_less_than<MyString,
669/// const char*> {
670/// ...
671/// // Only need to define less-than. Others defined in terms of this.
672/// friend bool operator<(const MyString& a, const char* b) noexcept;
673/// friend bool operator<(const char* a, const MyString& b) noexcept;
674/// };
675/// @endcode
676template <typename Derived, typename Other = Derived>
678 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function"
679 /// @brief Greater-than-or-equal comparison operator for instances of @c LhsType and
680 /// @c RhsType.
681 /// @param lhs The first object being compared
682 /// @param rhs The second object being compared
683 /// @return @c True if @c lhs is greater than or equal to @c rhs, @c false
684 /// otherwise.
685 ARENE_NODISCARD friend constexpr auto operator==(Derived const& lhs, Other const& rhs) noexcept -> bool {
686 static_assert(is_nothrow_less_than_comparable_v<Derived const&, Other const&>, "lhs < rhs should be noexcept");
687 static_assert(is_nothrow_less_than_comparable_v<Other const&, Derived const&>, "rhs < lhs should be noexcept");
688 return !static_cast<bool>(lhs < rhs) && !static_cast<bool>(rhs < lhs);
689 }
690 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
691};
692
693/// @brief An enumeration used for the return value of @c fast_inequality_check functions to indicate if two values are
694/// definitely not equal, or may be equal or not. This is used in generic implementations of @c operator== and @c
695/// operator!= to avoid comparing for ordering when only equality is required.
696enum class inequality_heuristic : bool {
697 ///@brief The two values checked are definitely not equal to each other
699 ///@brief The values checked may or may not be equal to each other; a more detailed check is required
701};
702
703/// @brief Type trait to check if an instance of type @c T can be compared to an instance of type @c U with a static
704/// @c fast_inequality_check member function that returns @c inequality_heuristic.
705/// @tparam T The type of the lhs
706/// @tparam U The type of the rhs. Defaults to @c T
707/// @return true If @c T::fast_inequality_check(T,U) is defined and returns @c inequality_heuristic.
708/// @return false Otherwise.
709template <typename T, typename U = T, typename = constraints<>>
710extern constexpr bool has_fast_inequality_check_v = false;
711
712/// @brief Specialization for the case that @c T and @c U can be compared.
713template <typename T, typename U>
714extern constexpr bool has_fast_inequality_check_v<
715 T,
716 U,
717 constraints<std::enable_if_t<std::is_same<
718 decltype(T::fast_inequality_check(std::declval<T const&>(), std::declval<U const&>())),
719 inequality_heuristic>::value>>> = true;
720
721template <typename Derived>
722class generic_ordering_from_three_way_compare;
723
724template <typename Derived>
725class generic_ordering_from_three_way_compare_and_equals;
726
727namespace operators_detail {
728
729/// @brief Non-class types never derive from the mixin.
730/// @tparam Type The type to test
731template <typename Type, typename = constraints<>>
732extern constexpr bool uses_generic_three_way_v = false;
733
734/// @brief Specialization for class types, which are the only ones that can derive from the mixin.
735/// @tparam Type The type to test
736template <typename Type>
737extern constexpr bool uses_generic_three_way_v<Type, constraints<std::enable_if_t<std::is_class<Type>::value>>> =
739
740/// @brief Type trait detecting whether @c Type uses the @c generic_ordering_from_three_way_compare_and_equals mixin.
741/// @tparam Type The type to test
742template <typename Type, typename = constraints<>>
743extern constexpr bool uses_generic_three_way_and_equals_v = false;
744
745/// @brief Specialization for class types, which are the only ones that can derive from the mixin.
746/// @tparam Type The type to test
747template <typename Type>
748extern constexpr bool
749 uses_generic_three_way_and_equals_v<Type, constraints<std::enable_if_t<std::is_class<Type>::value>>> =
751
752/// @brief Type trait to check if we can provide a reversed operator== for this pair of types (breaks an infinite loop)
753/// @tparam Self The type which is inheriting from this operator mixin
754/// @tparam Other The type which we're trying to compare with (the LHS of a reversed comparison)
755template <typename Self, typename Other>
756extern constexpr bool eligible_for_reversed_equals_with_v =
757 !std::is_same<Self, Other>::value && comparison_traits_detail::is_equality_comparable_helper::test<Self, Other>(0);
758
759/// @brief Non-template base class of @c generic_ordering_from_three_way_compare that declares its
760/// comparison operators as hidden friends.
761///
762/// The operators are declared here rather than in @c generic_ordering_from_three_way_compare to avoid creating
763/// a set of hidden friend functions for every derived class. That caused a long compile time problem for gcc 8.3.0
764/// as the template resolution lookup became quadratic in the number of instantiations.
765class generic_ordering_tag {
766 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
767 /// @brief Less-than comparison for a type using generic_ordering_from_three_way_compare.
768 /// @tparam LhsType The type of the left-hand operand
769 /// @tparam RhsType The type of the right-hand operand
770 /// @param lhs The left-hand operand
771 /// @param rhs The right-hand operand
772 /// @return bool The result of the comparison
773 template <
774 typename LhsType,
775 typename RhsType,
776 constraints<
779 ARENE_NODISCARD friend constexpr auto operator<(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
780 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
781 // side-effects"
782 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
783 // parasoft-end-suppress CERT_C-PRE31-c-3
784 return LhsType::three_way_compare(lhs, rhs) == strong_ordering::less;
785 }
786 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
787
788 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
789 /// @brief Less-than comparison for a type using generic_ordering_from_three_way_compare.
790 /// @tparam LhsType The type of the left-hand operand
791 /// @tparam RhsType The type of the right-hand operand
792 /// @param lhs The left-hand operand
793 /// @param rhs The right-hand operand
794 /// @return bool The result of the comparison
795 template <
796 typename LhsType,
797 typename RhsType,
798 constraints<
802 ARENE_NODISCARD friend constexpr auto operator<(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
803 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
804 // side-effects"
805 static_assert(noexcept(RhsType::three_way_compare(rhs, lhs)), "three_way_compare must be noexcept");
806 // parasoft-end-suppress CERT_C-PRE31-c-3
807 return RhsType::three_way_compare(rhs, lhs) == strong_ordering::greater;
808 }
809 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
810
811 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
812 /// @brief Greater-than comparison for a type using generic_ordering_from_three_way_compare.
813 /// @tparam LhsType The type of the left-hand operand
814 /// @tparam RhsType The type of the right-hand operand
815 /// @param lhs The left-hand operand
816 /// @param rhs The right-hand operand
817 /// @return bool The result of the comparison
818 template <
819 typename LhsType,
820 typename RhsType,
824 ARENE_NODISCARD friend constexpr auto operator>(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
825 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
826 // side-effects"
827 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
828 // parasoft-end-suppress CERT_C-PRE31-c-3
829 return LhsType::three_way_compare(lhs, rhs) == strong_ordering::greater;
830 }
831 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
832
833 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
834 /// @brief Greater-than comparison for a type using generic_ordering_from_three_way_compare.
835 /// @tparam LhsType The type of the left-hand operand
836 /// @tparam RhsType The type of the right-hand operand
837 /// @param lhs The left-hand operand
838 /// @param rhs The right-hand operand
839 /// @return bool The result of the comparison
840 template <
841 typename LhsType,
842 typename RhsType,
847 ARENE_NODISCARD friend constexpr auto operator>(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
848 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
849 // side-effects"
850 static_assert(noexcept(RhsType::three_way_compare(rhs, lhs)), "three_way_compare must be noexcept");
851 // parasoft-end-suppress CERT_C-PRE31-c-3
852 return RhsType::three_way_compare(rhs, lhs) == strong_ordering::less;
853 }
854 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
855
856 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
857 /// @brief Less-than-or-equal comparison for a type using generic_ordering_from_three_way_compare.
858 /// @tparam LhsType The type of the left-hand operand
859 /// @tparam RhsType The type of the right-hand operand
860 /// @param lhs The left-hand operand
861 /// @param rhs The right-hand operand
862 /// @return bool The result of the comparison
863 template <
864 typename LhsType,
865 typename RhsType,
869 ARENE_NODISCARD friend constexpr auto operator<=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
870 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
871 // side-effects"
872 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
873 // parasoft-end-suppress CERT_C-PRE31-c-3
874 return LhsType::three_way_compare(lhs, rhs) != strong_ordering::greater;
875 }
876 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
877
878 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
879 /// @brief Less-than-or-equal comparison for a type using generic_ordering_from_three_way_compare.
880 /// @tparam LhsType The type of the left-hand operand
881 /// @tparam RhsType The type of the right-hand operand
882 /// @param lhs The left-hand operand
883 /// @param rhs The right-hand operand
884 /// @return bool The result of the comparison
885 template <
886 typename LhsType,
887 typename RhsType,
892 ARENE_NODISCARD friend constexpr auto operator<=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
893 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
894 // side-effects"
895 static_assert(noexcept(RhsType::three_way_compare(rhs, lhs)), "three_way_compare must be noexcept");
896 // parasoft-end-suppress CERT_C-PRE31-c-3
897 return RhsType::three_way_compare(rhs, lhs) != strong_ordering::less;
898 }
899 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
900
901 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
902 /// @brief Greater-than-or-equal comparison for a type using generic_ordering_from_three_way_compare.
903 /// @tparam LhsType The type of the left-hand operand
904 /// @tparam RhsType The type of the right-hand operand
905 /// @param lhs The left-hand operand
906 /// @param rhs The right-hand operand
907 /// @return bool The result of the comparison
908 template <
909 typename LhsType,
910 typename RhsType,
914 ARENE_NODISCARD friend constexpr auto operator>=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
915 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
916 // side-effects"
917 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
918 // parasoft-end-suppress CERT_C-PRE31-c-3
919 return LhsType::three_way_compare(lhs, rhs) != strong_ordering::less;
920 }
921 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
922
923 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
924 /// @brief Greater-than-or-equal comparison for a type using generic_ordering_from_three_way_compare.
925 /// @tparam LhsType The type of the left-hand operand
926 /// @tparam RhsType The type of the right-hand operand
927 /// @param lhs The left-hand operand
928 /// @param rhs The right-hand operand
929 /// @return bool The result of the comparison
930 template <
931 typename LhsType,
932 typename RhsType,
937 ARENE_NODISCARD friend constexpr auto operator>=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
938 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
939 // side-effects"
940 static_assert(noexcept(RhsType::three_way_compare(rhs, lhs)), "three_way_compare must be noexcept");
941 // parasoft-end-suppress CERT_C-PRE31-c-3
942 return RhsType::three_way_compare(rhs, lhs) != strong_ordering::greater;
943 }
944 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
945
946 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
947 /// @brief Equality comparison, without a fast-inequality heuristic.
948 /// @tparam LhsType The type of the left-hand operand
949 /// @tparam RhsType The type of the right-hand operand
950 /// @param lhs The left-hand operand
951 /// @param rhs The right-hand operand
952 /// @return bool The result of the comparison
953 template <
954 typename LhsType,
955 typename RhsType,
960 ARENE_NODISCARD friend constexpr auto operator==(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
961 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
962 // side-effects"
963 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
964 // parasoft-end-suppress CERT_C-PRE31-c-3
965 return LhsType::three_way_compare(lhs, rhs) == strong_ordering::equal;
966 }
967 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
968
969 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
970 /// @brief Equality comparison, using the fast-inequality heuristic.
971 /// @tparam LhsType The type of the left-hand operand
972 /// @tparam RhsType The type of the right-hand operand
973 /// @param lhs The left-hand operand
974 /// @param rhs The right-hand operand
975 /// @return bool The result of the comparison
976 template <
977 typename LhsType,
978 typename RhsType,
983 ARENE_NODISCARD friend constexpr auto operator==(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
984 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
985 // side-effects"
986 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
987 static_assert(noexcept(LhsType::fast_inequality_check(lhs, rhs)), "fast_inequality_check must be noexcept");
988 // parasoft-end-suppress CERT_C-PRE31-c-3
989 return (LhsType::fast_inequality_check(lhs, rhs) == inequality_heuristic::may_be_equal_or_not_equal) &&
990 (LhsType::three_way_compare(lhs, rhs) == strong_ordering::equal);
991 }
992 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
993
994 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
995 /// @brief Reversed equality comparison.
996 /// @tparam LhsType The type of the left-hand operand
997 /// @tparam RhsType The type of the right-hand operand
998 /// @param lhs The left-hand operand
999 /// @param rhs The right-hand operand
1000 /// @return bool The result of the comparison
1001 template <
1002 typename LhsType,
1003 typename RhsType,
1008 ARENE_NODISCARD friend constexpr auto operator==(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1009 return rhs == lhs;
1010 }
1011 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1012
1013 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1014 /// @brief Inequality comparison.
1015 /// @tparam LhsType The type of the left-hand operand
1016 /// @tparam RhsType The type of the right-hand operand
1017 /// @param lhs The left-hand operand
1018 /// @param rhs The right-hand operand
1019 /// @return bool The result of the comparison
1020 template <
1021 typename LhsType,
1022 typename RhsType,
1026 ARENE_NODISCARD friend constexpr auto operator!=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1027 return !(lhs == rhs);
1028 }
1029 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1030
1031 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1032 /// @brief Reversed inequality comparison.
1033 /// @tparam LhsType The type of the left-hand operand
1034 /// @tparam RhsType The type of the right-hand operand
1035 /// @param lhs The left-hand operand
1036 /// @param rhs The right-hand operand
1037 /// @return bool The result of the comparison
1038 template <
1039 typename LhsType,
1040 typename RhsType,
1045 ARENE_NODISCARD friend constexpr auto operator!=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1046 return !(lhs == rhs);
1047 }
1048 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1049
1050 protected:
1051 /// @brief Default constructor is protected
1052 constexpr generic_ordering_tag() noexcept = default;
1053 /// @brief Default destructor is protected
1054 ~generic_ordering_tag() noexcept = default;
1055 /// @brief Default copy constructor is protected
1056 /// @param other The source object
1057 constexpr generic_ordering_tag(generic_ordering_tag const& other) noexcept = default;
1058 /// @brief Default move constructor is protected
1059 /// @param other The source object
1060 constexpr generic_ordering_tag(generic_ordering_tag&& other) noexcept = default;
1061 /// @brief Default copy assignment is protected
1062 /// @param other The source object
1063 constexpr auto operator=(generic_ordering_tag const& other) & noexcept -> generic_ordering_tag& = default;
1064 /// @brief Default move assignment is protected
1065 /// @param other The source object
1066 constexpr auto operator=(generic_ordering_tag&& other) & noexcept -> generic_ordering_tag& = default;
1067};
1068
1069/// @brief Non-template base class of @c generic_ordering_from_three_way_compare_and_equals that declares its
1070/// comparison operators as hidden friends.
1071///
1072/// The operators are declared here rather than in @c generic_ordering_from_three_way_compare_and_equals.
1073/// See the comment in @c generic_ordering_tag.
1074class generic_ordering_and_equals_tag {
1075 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1076 /// @brief Less-than comparison for a type using generic_ordering_from_three_way_compare_and_equals.
1077 /// @tparam LhsType The type of the left-hand operand
1078 /// @tparam RhsType The type of the right-hand operand
1079 /// @param lhs The left-hand operand
1080 /// @param rhs The right-hand operand
1081 /// @return bool The result of the comparison
1082 template <
1083 typename LhsType,
1084 typename RhsType,
1088 ARENE_NODISCARD friend constexpr auto operator<(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1089 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
1090 // side-effects"
1091 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
1092 // parasoft-end-suppress CERT_C-PRE31-c-3
1093 return LhsType::three_way_compare(lhs, rhs) == strong_ordering::less;
1094 }
1095 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1096
1097 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1098 /// @brief Less-than comparison for a type using generic_ordering_from_three_way_compare_and_equals.
1099 /// @tparam LhsType The type of the left-hand operand
1100 /// @tparam RhsType The type of the right-hand operand
1101 /// @param lhs The left-hand operand
1102 /// @param rhs The right-hand operand
1103 /// @return bool The result of the comparison
1104 template <
1105 typename LhsType,
1106 typename RhsType,
1111 ARENE_NODISCARD friend constexpr auto operator<(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1112 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
1113 // side-effects"
1114 static_assert(noexcept(RhsType::three_way_compare(rhs, lhs)), "three_way_compare must be noexcept");
1115 // parasoft-end-suppress CERT_C-PRE31-c-3
1116 return RhsType::three_way_compare(rhs, lhs) == strong_ordering::greater;
1117 }
1118 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1119
1120 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1121 /// @brief Greater-than comparison for a type using generic_ordering_from_three_way_compare_and_equals.
1122 /// @tparam LhsType The type of the left-hand operand
1123 /// @tparam RhsType The type of the right-hand operand
1124 /// @param lhs The left-hand operand
1125 /// @param rhs The right-hand operand
1126 /// @return bool The result of the comparison
1127 template <
1128 typename LhsType,
1129 typename RhsType,
1133 ARENE_NODISCARD friend constexpr auto operator>(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1134 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
1135 // side-effects"
1136 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
1137 // parasoft-end-suppress CERT_C-PRE31-c-3
1138 return LhsType::three_way_compare(lhs, rhs) == strong_ordering::greater;
1139 }
1140 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1141
1142 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1143 /// @brief Greater-than comparison for a type using generic_ordering_from_three_way_compare_and_equals.
1144 /// @tparam LhsType The type of the left-hand operand
1145 /// @tparam RhsType The type of the right-hand operand
1146 /// @param lhs The left-hand operand
1147 /// @param rhs The right-hand operand
1148 /// @return bool The result of the comparison
1149 template <
1150 typename LhsType,
1151 typename RhsType,
1156 ARENE_NODISCARD friend constexpr auto operator>(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1157 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
1158 // side-effects"
1159 static_assert(noexcept(RhsType::three_way_compare(rhs, lhs)), "three_way_compare must be noexcept");
1160 // parasoft-end-suppress CERT_C-PRE31-c-3
1161 return RhsType::three_way_compare(rhs, lhs) == strong_ordering::less;
1162 }
1163 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1164
1165 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1166 /// @brief Less-than-or-equal comparison for a type using generic_ordering_from_three_way_compare_and_equals.
1167 /// @tparam LhsType The type of the left-hand operand
1168 /// @tparam RhsType The type of the right-hand operand
1169 /// @param lhs The left-hand operand
1170 /// @param rhs The right-hand operand
1171 /// @return bool The result of the comparison
1172 template <
1173 typename LhsType,
1174 typename RhsType,
1178 ARENE_NODISCARD friend constexpr auto operator<=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1179 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
1180 // side-effects"
1181 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
1182 // parasoft-end-suppress CERT_C-PRE31-c-3
1183 return LhsType::three_way_compare(lhs, rhs) != strong_ordering::greater;
1184 }
1185 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1186
1187 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1188 /// @brief Less-than-or-equal comparison for a type using generic_ordering_from_three_way_compare_and_equals.
1189 /// @tparam LhsType The type of the left-hand operand
1190 /// @tparam RhsType The type of the right-hand operand
1191 /// @param lhs The left-hand operand
1192 /// @param rhs The right-hand operand
1193 /// @return bool The result of the comparison
1194 template <
1195 typename LhsType,
1196 typename RhsType,
1201 ARENE_NODISCARD friend constexpr auto operator<=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1202 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
1203 // side-effects"
1204 static_assert(noexcept(RhsType::three_way_compare(rhs, lhs)), "three_way_compare must be noexcept");
1205 // parasoft-end-suppress CERT_C-PRE31-c-3
1206 return RhsType::three_way_compare(rhs, lhs) != strong_ordering::less;
1207 }
1208 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1209
1210 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1211 /// @brief Greater-than-or-equal comparison for a type using generic_ordering_from_three_way_compare_and_equals.
1212 /// @tparam LhsType The type of the left-hand operand
1213 /// @tparam RhsType The type of the right-hand operand
1214 /// @param lhs The left-hand operand
1215 /// @param rhs The right-hand operand
1216 /// @return bool The result of the comparison
1217 template <
1218 typename LhsType,
1219 typename RhsType,
1223 ARENE_NODISCARD friend constexpr auto operator>=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1224 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
1225 // side-effects"
1226 static_assert(noexcept(LhsType::three_way_compare(lhs, rhs)), "three_way_compare must be noexcept");
1227 // parasoft-end-suppress CERT_C-PRE31-c-3
1228 return LhsType::three_way_compare(lhs, rhs) != strong_ordering::less;
1229 }
1230 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1231
1232 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1233 /// @brief Greater-than-or-equal comparison for a type using generic_ordering_from_three_way_compare_and_equals.
1234 /// @tparam LhsType The type of the left-hand operand
1235 /// @tparam RhsType The type of the right-hand operand
1236 /// @param lhs The left-hand operand
1237 /// @param rhs The right-hand operand
1238 /// @return bool The result of the comparison
1239 template <
1240 typename LhsType,
1241 typename RhsType,
1246 ARENE_NODISCARD friend constexpr auto operator>=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1247 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
1248 // side-effects"
1249 static_assert(noexcept(RhsType::three_way_compare(rhs, lhs)), "three_way_compare must be noexcept");
1250 // parasoft-end-suppress CERT_C-PRE31-c-3
1251 return RhsType::three_way_compare(rhs, lhs) != strong_ordering::greater;
1252 }
1253 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1254
1255 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1256 /// @brief Reversed equality comparison, delegating to the derived class's operator==.
1257 /// @tparam LhsType The type of the left-hand operand
1258 /// @tparam RhsType The type of the right-hand operand
1259 /// @param lhs The left-hand operand
1260 /// @param rhs The right-hand operand
1261 /// @return bool The result of the comparison
1262 template <
1263 typename LhsType,
1264 typename RhsType,
1268 ARENE_NODISCARD friend constexpr auto operator==(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1269 return rhs == lhs;
1270 }
1271 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1272
1273 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1274 /// @brief Inequality comparison, delegating to the derived class's operator==.
1275 /// @tparam LhsType The type of the left-hand operand
1276 /// @tparam RhsType The type of the right-hand operand
1277 /// @param lhs The left-hand operand
1278 /// @param rhs The right-hand operand
1279 /// @return bool The result of the comparison
1280 template <
1281 typename LhsType,
1282 typename RhsType,
1286 ARENE_NODISCARD friend constexpr auto operator!=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1287 return !(lhs == rhs);
1288 }
1289 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1290
1291 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1292 /// @brief Reversed inequality comparison, delegating to the derived class's operator==.
1293 /// @tparam LhsType The type of the left-hand operand
1294 /// @tparam RhsType The type of the right-hand operand
1295 /// @param lhs The left-hand operand
1296 /// @param rhs The right-hand operand
1297 /// @return bool The result of the comparison
1298 template <
1299 typename LhsType,
1300 typename RhsType,
1304 ARENE_NODISCARD friend constexpr auto operator!=(LhsType const& lhs, RhsType const& rhs) noexcept -> bool {
1305 return !(lhs == rhs);
1306 }
1307 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1308
1309 protected:
1310 /// @brief Default constructor is protected
1311 constexpr generic_ordering_and_equals_tag() noexcept = default;
1312 /// @brief Default destructor is protected
1313 ~generic_ordering_and_equals_tag() noexcept = default;
1314 /// @brief Default copy constructor is protected
1315 /// @param other The source object
1316 constexpr generic_ordering_and_equals_tag(generic_ordering_and_equals_tag const& other) noexcept = default;
1317 /// @brief Default move constructor is protected
1318 /// @param other The source object
1319 constexpr generic_ordering_and_equals_tag(generic_ordering_and_equals_tag&& other) noexcept = default;
1320 /// @brief Default copy assignment is protected
1321 /// @param other The source object
1322 constexpr auto operator=(generic_ordering_and_equals_tag const& other) & noexcept
1323 -> generic_ordering_and_equals_tag& = default;
1324 /// @brief Default move assignment is protected
1325 /// @param other The source object
1326 constexpr auto operator=(generic_ordering_and_equals_tag&& other) & noexcept
1327 -> generic_ordering_and_equals_tag& = default;
1328};
1329
1330} // namespace operators_detail
1331
1332/// @brief Helper class that can be inherited from by a class to define the relational comparison operators in terms of
1333/// a @c three_way_compare function provided by the @c Derived class, and an optional fast-inequality check heuristic.
1334///
1335/// Generates the @c <, @c >, @c <=, @c >=, @c ==, and @c != comparison operators from a possibly overloaded @c static
1336/// member function @c three_way_compare in the @c Derived class that accepts a <c>const Derived&</c> as the first
1337/// argument and any other type as the second argument, and returns a @c strong_ordering value indicating whether the
1338/// first is less than, equal to or greater than the second. These @c three_way_compare functions must not throw
1339/// exceptions.
1340///
1341/// The derived class can optionally provide a @c static member function @c fast_inequality_check that accepts a
1342/// <c>const Derived&</c> as the first argument and any other type as the second argument, and returns a @c
1343/// inequality_heuristic value indicating whether the first is definitely not equal to the second, or may or may not be
1344/// equal to the second. These @c fast_inequality_check functions must not throw exceptions. The generated @c operator==
1345/// and @c operator!= overloads will invoke @c fast_inequality_check prior to @c three_way_compare if there is an
1346/// overload provided that accepts the given types.
1347///
1348/// @tparam Derived The type of the derived class. The comparison operators provided take parameters of this type.
1349///
1350/// For example:
1351/// @snippet docs/examples/ordering_examples.cpp string_like_generic_example
1352///
1353/// Here, @c MyClass has comparison operators for comparing instances of @c MyClass with each other, or with @c
1354/// OtherClass, or with @c YetAnotherClass. Equality comparisons with @c OtherClass will use @c fast_inequality_check to
1355/// avoid calling a potentially expensive @c three_way_compare for instances that are definitely not equal. e.g. this
1356/// may be used to check the size of a container, and avoid doing elementwise comparisons when ordering is not required.
1357template <typename Derived>
1358class generic_ordering_from_three_way_compare : private operators_detail::generic_ordering_tag {
1359 // The comparison operators are declared as hidden friends of the non-template base class
1360 protected:
1361 /// @brief Default constructor is protected
1362 constexpr generic_ordering_from_three_way_compare() noexcept = default;
1363 /// @brief Default destructor is protected
1365 /// @brief Default copy constructor is protected
1366 /// @param other The source object
1368 ) noexcept = default;
1369 /// @brief Default move constructor is protected
1370 /// @param other The source object
1372 /// @brief Default copy assignment is protected
1373 /// @param other The source object
1374 constexpr auto operator=(generic_ordering_from_three_way_compare const& other) & noexcept
1376 /// @brief Default move assignment is protected
1377 /// @param other The source object
1378 constexpr auto operator=(generic_ordering_from_three_way_compare&& other) & noexcept
1380};
1381
1382/// @brief Helper class that can be inherited from by a class to define the relational comparison operators in terms of
1383/// a @c three_way_compare function provided by the @c Derived class, but preferentially implementing @c == and @c !=
1384/// to use <c>operator==(Derived, Other)</c> rather than <c>three_way_compare</c>.
1385///
1386/// Generates the @c <, @c >, @c <=, and @c >= comparison operators from a possibly overloaded @c static member function
1387/// @c three_way_compare in the @c Derived class that accepts a <c>const Derived&</c> as the first argument and any
1388/// other type as the second argument, and returns a @c strong_ordering value indicating whether the first is less than,
1389/// equal to or greater than the second. These @c three_way_compare functions must not throw exceptions.
1390///
1391/// Even if the derived class provides a @c fast_inequality_check function, it is not used, because the exact @c
1392/// operator== is preferred over <c>three_way_compare</c>.
1393///
1394/// @tparam Derived The type of the derived class. The comparison operators provided take parameters of this type.
1395///
1396/// For example:
1397/// @snippet docs/examples/ordering_examples.cpp three_way_and_equals_mixin_example
1398///
1399/// Here, @c MyClass has comparison operators for comparing instances of @c MyClass with each other, or with @c
1400/// OtherClass, or with @c YetAnotherClass. Equality comparisons with @c OtherClass will use <c>operator==</c>, while
1401/// comparisons with @c YetAnotherClass will use <c>three_way_compare</c>.
1402template <typename Derived>
1403class generic_ordering_from_three_way_compare_and_equals : private operators_detail::generic_ordering_and_equals_tag {
1404 // The comparison operators are declared as hidden friends of the non-template base class
1405 protected:
1406 /// @brief Default constructor is protected
1408 /// @brief Default destructor is protected
1410 /// @brief Default copy constructor is protected
1411 /// @param other The source object
1414 ) noexcept = default;
1415 /// @brief Default move constructor is protected
1416 /// @param other The source object
1419 ) noexcept = default;
1420 /// @brief Default copy assignment is protected
1421 /// @param other The source object
1422 constexpr auto operator=(generic_ordering_from_three_way_compare_and_equals const& other) & noexcept
1424 /// @brief Default move assignment is protected
1425 /// @param other The source object
1426 constexpr auto operator=(generic_ordering_from_three_way_compare_and_equals&& other) & noexcept
1428};
1429
1430} // namespace base
1431} // namespace arene
1432
1433// parasoft-end-suppress AUTOSAR-A11_3_1-a-2
1434// parasoft-end-suppress AUTOSAR-A13_5_5-b-2
1435
1436#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPARE_OPERATORS_HPP_
constexpr auto operator=(full_equality_operators_from_basic_equality const &other) &noexcept -> full_equality_operators_from_basic_equality &=default
Default copy assignment is protected.
ARENE_NODISCARD friend constexpr auto operator==(Other const &lhs, Derived const &rhs) noexcept -> bool
Equality comparison operator for instances of Derived and Other.
Definition operators.hpp:105
constexpr full_equality_operators_from_basic_equality() noexcept=default
Default constructor is protected.
constexpr full_ordering_operators_from_less_than_and_equals() noexcept=default
Default constructor is protected.
constexpr auto operator=(full_ordering_operators_from_less_than_and_equals const &other) &noexcept -> full_ordering_operators_from_less_than_and_equals &=default
Helper class that can be inherited from by a class to define the >, <=, >=, == and !...
Definition operators.hpp:677
ARENE_NODISCARD friend constexpr auto operator==(Derived const &lhs, Other const &rhs) noexcept -> bool
Greater-than-or-equal comparison operator for instances of LhsType and RhsType.
Definition operators.hpp:685
ARENE_NODISCARD friend constexpr auto operator<=(Other const &lhs, Derived const &rhs) noexcept -> bool
Less-than-or-equal comparison operator for instances of Derived and Other.
Definition operators.hpp:334
ARENE_NODISCARD friend constexpr auto operator>(Other const &lhs, Derived const &rhs) noexcept -> bool
Greater-than comparison operator for instances of Derived and Other.
Definition operators.hpp:323
ARENE_NODISCARD friend constexpr auto operator>=(Other const &lhs, Derived const &rhs) noexcept -> bool
Greater-than-or-equal comparison operator for instances of Derived and Other.
Definition operators.hpp:345
constexpr auto operator=(full_ordering_operators_from_three_way_compare_and_equals const &other) &noexcept -> full_ordering_operators_from_three_way_compare_and_equals &=default
Default copy assignment is protected.
ARENE_NODISCARD friend constexpr auto operator<(Other const &lhs, Derived const &rhs) noexcept -> bool
Less-than comparison operator for instances of Derived and Other.
Definition operators.hpp:313
constexpr full_ordering_operators_from_three_way_compare_and_equals() noexcept=default
Default constructor is protected.
constexpr full_ordering_operators_from_three_way_compare() noexcept=default
Default constructor is protected.
ARENE_NODISCARD friend constexpr auto operator==(Derived const &lhs, Other const &rhs) noexcept -> bool
Equality comparison operator for instances of Derived and Other.
Definition operators.hpp:448
constexpr auto operator=(full_ordering_operators_from_three_way_compare const &other) &noexcept -> full_ordering_operators_from_three_way_compare &=default
Default copy assignment is protected.
constexpr generic_ordering_from_three_way_compare_and_equals() noexcept=default
Default constructor is protected.
constexpr generic_ordering_from_three_way_compare() noexcept=default
Default constructor is protected.
static constexpr value_type value
Definition integral_constant.hpp:34
Type trait to detect if Base is a base class of Derived.
Definition is_base_of.hpp:30
Trait to detect if the two template parameters are the same or not. The value member is true if the t...
Definition is_same.hpp:35
#define ARENE_DEPRECATED_WITH(...)
An alias for the [[deprecated(...)]] attribute, or a no-op if the attribute is not supported.
Definition deprecated.hpp:34
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
strong_ordering_detail::strong_ordering strong_ordering
The enumeration specifying the result of an ordering comparison.
Definition strong_ordering.hpp:147
ARENE_IGNORE_ALL("-Wdeprecated-declarations", "We still want to use the deprecated class in this other deprecated class")
constexpr bool is_nothrow_equality_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
inequality_heuristic
An enumeration used for the return value of fast_inequality_check functions to indicate if two values...
Definition operators.hpp:696
@ may_be_equal_or_not_equal
The values checked may or may not be equal to each other; a more detailed check is required.
Definition operators.hpp:700
@ definitely_not_equal
The two values checked are definitely not equal to each other.
Definition operators.hpp:698
constexpr bool is_nothrow_less_than_comparable_v
Definition accumulate.hpp:36
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
#define ARENE_NODISCARD
An alias for [[nodiscard]] , or a no-op if the attribute is not supported.
Definition nodiscard.hpp:26