Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcompare.h
Go to the documentation of this file.
1// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
2// Copyright (C) 2023 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QCOMPARE_H
6#define QCOMPARE_H
7
8#if 0
9#pragma qt_class(QtCompare)
10#endif
11
12#include <QtCore/qglobal.h>
13#include <QtCore/qcompare_impl.h>
14#include <QtCore/qstdlibdetection.h>
15
16#ifdef __cpp_lib_bit_cast
17#include <bit>
18#endif
19#ifdef __cpp_lib_three_way_comparison
20#include <compare>
21#endif
22
23QT_BEGIN_NAMESPACE
24
25namespace QtPrivate {
27constexpr CompareUnderlyingType LegacyUncomparableValue = -127; // historic Qt value
28
29// [cmp.categories.pre] / 1
37
39{
41 #if defined(Q_STL_LIBCPP)
42 -127
43 #elif defined(Q_STL_LIBSTDCPP)
44 2
45 #elif defined(Q_STL_MSSTL)
46 -128
47 #elif defined(Q_STL_DINKUMWARE) ||
48 defined(Q_STL_ROGUEWAVE) ||
49 defined(Q_STL_STLPORT) ||
50 defined(Q_STL_SGI)
52 // We haven't seen C++20 of these libraries, so we don't promise BC there.
53 # ifdef __cpp_lib_three_way_comparison
54 # error Please report the numeric value of std::partial_ordering::unordered in your STL in a bug report.
55 # endif
56 #else
57 # error Please handle any newly-added Q_STL_ checks in the above ifdef-ery.
58 #endif
59};
60
61} // namespace QtPrivate
62
64
65template <typename O>
66constexpr O reversed(O o) noexcept
67{
68 // https://eel.is/c++draft/cmp.partialord#5
69 return is_lt(o) ? O::greater :
70 is_gt(o) ? O::less :
71 /*else*/ o ;
72}
73
74} // namespace QtOrderingPrivate
75
76namespace Qt {
77
78class weak_ordering;
79class strong_ordering;
80
82{
83public:
84 static const partial_ordering less;
88
89 friend constexpr bool operator==(partial_ordering lhs,
91 { return lhs.isOrdered() && lhs.m_order == 0; }
92
93 friend constexpr bool operator!=(partial_ordering lhs,
95 { return !lhs.isOrdered() || lhs.m_order != 0; }
96
97 friend constexpr bool operator< (partial_ordering lhs,
99 { return lhs.isOrdered() && lhs.m_order < 0; }
100
101 friend constexpr bool operator<=(partial_ordering lhs,
103 { return lhs.isOrdered() && lhs.m_order <= 0; }
104
105 friend constexpr bool operator> (partial_ordering lhs,
107 { return lhs.isOrdered() && lhs.m_order > 0; }
108
109 friend constexpr bool operator>=(partial_ordering lhs,
111 { return lhs.isOrdered() && lhs.m_order >= 0; }
112
113
115 partial_ordering rhs) noexcept
116 { return rhs.isOrdered() && 0 == rhs.m_order; }
117
119 partial_ordering rhs) noexcept
120 { return !rhs.isOrdered() || 0 != rhs.m_order; }
121
123 partial_ordering rhs) noexcept
124 { return rhs.isOrdered() && 0 < rhs.m_order; }
125
127 partial_ordering rhs) noexcept
128 { return rhs.isOrdered() && 0 <= rhs.m_order; }
129
131 partial_ordering rhs) noexcept
132 { return rhs.isOrdered() && 0 > rhs.m_order; }
133
135 partial_ordering rhs) noexcept
136 { return rhs.isOrdered() && 0 >= rhs.m_order; }
137
138
139#ifdef __cpp_lib_three_way_comparison
140 friend constexpr std::partial_ordering
142 { return lhs; } // https://eel.is/c++draft/cmp.partialord#4
143
144 friend constexpr std::partial_ordering
146 { return QtOrderingPrivate::reversed(rhs); }
147#endif // __cpp_lib_three_way_comparison
148
149
150 friend constexpr bool operator==(partial_ordering lhs, partial_ordering rhs) noexcept
151 { return lhs.m_order == rhs.m_order; }
152
153 friend constexpr bool operator!=(partial_ordering lhs, partial_ordering rhs) noexcept
154 { return lhs.m_order != rhs.m_order; }
155
156#ifdef __cpp_lib_three_way_comparison
158 {
163 else if (stdorder == std::partial_ordering::greater)
167 }
168
169 constexpr Q_IMPLICIT operator std::partial_ordering() const noexcept
170 {
171 static_assert(sizeof(*this) == sizeof(std::partial_ordering));
172#ifdef __cpp_lib_bit_cast
173 return std::bit_cast<std::partial_ordering>(*this);
174#else
175 using O = QtPrivate::Ordering;
176 using U = QtPrivate::Uncomparable;
177 using R = std::partial_ordering;
178 switch (m_order) {
179 case qToUnderlying(O::Less): return R::less;
180 case qToUnderlying(O::Greater): return R::greater;
181 case qToUnderlying(O::Equivalent): return R::equivalent;
182 case qToUnderlying(U::Unordered): return R::unordered;
183 }
185#endif // __cpp_lib_bit_cast
186 }
187
188 friend constexpr bool operator==(partial_ordering lhs, std::partial_ordering rhs) noexcept
189 { return static_cast<std::partial_ordering>(lhs) == rhs; }
190
191 friend constexpr bool operator!=(partial_ordering lhs, std::partial_ordering rhs) noexcept
192 { return static_cast<std::partial_ordering>(lhs) != rhs; }
193
194 friend constexpr bool operator==(std::partial_ordering lhs, partial_ordering rhs) noexcept
195 { return lhs == static_cast<std::partial_ordering>(rhs); }
196
197 friend constexpr bool operator!=(std::partial_ordering lhs, partial_ordering rhs) noexcept
198 { return lhs != static_cast<std::partial_ordering>(rhs); }
199
200 friend constexpr bool operator==(partial_ordering lhs, std::strong_ordering rhs) noexcept
201 { return static_cast<std::partial_ordering>(lhs) == rhs; }
202
203 friend constexpr bool operator!=(partial_ordering lhs, std::strong_ordering rhs) noexcept
204 { return static_cast<std::partial_ordering>(lhs) != rhs; }
205
206 friend constexpr bool operator==(std::strong_ordering lhs, partial_ordering rhs) noexcept
207 { return lhs == static_cast<std::partial_ordering>(rhs); }
208
209 friend constexpr bool operator!=(std::strong_ordering lhs, partial_ordering rhs) noexcept
210 { return lhs != static_cast<std::partial_ordering>(rhs); }
211
212 friend constexpr bool operator==(partial_ordering lhs, std::weak_ordering rhs) noexcept
213 { return static_cast<std::partial_ordering>(lhs) == rhs; }
214
215 friend constexpr bool operator!=(partial_ordering lhs, std::weak_ordering rhs) noexcept
216 { return static_cast<std::partial_ordering>(lhs) != rhs; }
217
218 friend constexpr bool operator==(std::weak_ordering lhs, partial_ordering rhs) noexcept
219 { return lhs == static_cast<std::partial_ordering>(rhs); }
220
221 friend constexpr bool operator!=(std::weak_ordering lhs, partial_ordering rhs) noexcept
222 { return lhs != static_cast<std::partial_ordering>(rhs); }
223#endif // __cpp_lib_three_way_comparison
224
225private:
226 friend class weak_ordering;
227 friend class strong_ordering;
228
229 constexpr explicit partial_ordering(QtPrivate::Ordering order) noexcept
230 : m_order(static_cast<QtPrivate::CompareUnderlyingType>(order))
231 {}
232 constexpr explicit partial_ordering(QtPrivate::Uncomparable order) noexcept
233 : m_order(static_cast<QtPrivate::CompareUnderlyingType>(order))
234 {}
235
236 QT_WARNING_PUSH
237 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100903
238 QT_WARNING_DISABLE_GCC("-Wzero-as-null-pointer-constant")
239 QT_WARNING_DISABLE_CLANG("-Wzero-as-null-pointer-constant")
240 friend constexpr bool is_eq (partial_ordering o) noexcept { return o == 0; }
241 friend constexpr bool is_neq (partial_ordering o) noexcept { return o != 0; }
242 friend constexpr bool is_lt (partial_ordering o) noexcept { return o < 0; }
243 friend constexpr bool is_lteq(partial_ordering o) noexcept { return o <= 0; }
244 friend constexpr bool is_gt (partial_ordering o) noexcept { return o > 0; }
245 friend constexpr bool is_gteq(partial_ordering o) noexcept { return o >= 0; }
247
248 // instead of the exposition only is_ordered member in [cmp.partialord],
249 // use a private function
250 constexpr bool isOrdered() const noexcept
252
254};
255
260
262{
263public:
264 static const weak_ordering less;
266 static const weak_ordering greater;
267
268 constexpr Q_IMPLICIT operator partial_ordering() const noexcept
269 { return partial_ordering(static_cast<QtPrivate::Ordering>(m_order)); }
270
271 friend constexpr bool operator==(weak_ordering lhs,
273 { return lhs.m_order == 0; }
274
275 friend constexpr bool operator!=(weak_ordering lhs,
277 { return lhs.m_order != 0; }
278
279 friend constexpr bool operator< (weak_ordering lhs,
281 { return lhs.m_order < 0; }
282
283 friend constexpr bool operator<=(weak_ordering lhs,
285 { return lhs.m_order <= 0; }
286
287 friend constexpr bool operator> (weak_ordering lhs,
289 { return lhs.m_order > 0; }
290
291 friend constexpr bool operator>=(weak_ordering lhs,
293 { return lhs.m_order >= 0; }
294
295
297 weak_ordering rhs) noexcept
298 { return 0 == rhs.m_order; }
299
301 weak_ordering rhs) noexcept
302 { return 0 != rhs.m_order; }
303
305 weak_ordering rhs) noexcept
306 { return 0 < rhs.m_order; }
307
309 weak_ordering rhs) noexcept
310 { return 0 <= rhs.m_order; }
311
313 weak_ordering rhs) noexcept
314 { return 0 > rhs.m_order; }
315
317 weak_ordering rhs) noexcept
318 { return 0 >= rhs.m_order; }
319
320
321#ifdef __cpp_lib_three_way_comparison
322 friend constexpr std::weak_ordering
324 { return lhs; } // https://eel.is/c++draft/cmp.weakord#5
325
326 friend constexpr std::weak_ordering
328 { return QtOrderingPrivate::reversed(rhs); }
329#endif // __cpp_lib_three_way_comparison
330
331
332 friend constexpr bool operator==(weak_ordering lhs, weak_ordering rhs) noexcept
333 { return lhs.m_order == rhs.m_order; }
334
335 friend constexpr bool operator!=(weak_ordering lhs, weak_ordering rhs) noexcept
336 { return lhs.m_order != rhs.m_order; }
337
338 friend constexpr bool operator==(weak_ordering lhs, partial_ordering rhs) noexcept
339 { return static_cast<partial_ordering>(lhs) == rhs; }
340
341 friend constexpr bool operator!=(weak_ordering lhs, partial_ordering rhs) noexcept
342 { return static_cast<partial_ordering>(lhs) != rhs; }
343
344 friend constexpr bool operator==(partial_ordering lhs, weak_ordering rhs) noexcept
345 { return lhs == static_cast<partial_ordering>(rhs); }
346
347 friend constexpr bool operator!=(partial_ordering lhs, weak_ordering rhs) noexcept
348 { return lhs != static_cast<partial_ordering>(rhs); }
349
350#ifdef __cpp_lib_three_way_comparison
352 {
355 else if (stdorder == std::weak_ordering::equivalent)
357 else if (stdorder == std::weak_ordering::greater)
359 }
360
361 constexpr Q_IMPLICIT operator std::weak_ordering() const noexcept
362 {
363 static_assert(sizeof(*this) == sizeof(std::weak_ordering));
364#ifdef __cpp_lib_bit_cast
365 return std::bit_cast<std::weak_ordering>(*this);
366#else
367 using O = QtPrivate::Ordering;
368 using R = std::weak_ordering;
369 switch (m_order) {
370 case qToUnderlying(O::Less): return R::less;
371 case qToUnderlying(O::Greater): return R::greater;
372 case qToUnderlying(O::Equivalent): return R::equivalent;
373 }
375#endif // __cpp_lib_bit_cast
376 }
377
378 friend constexpr bool operator==(weak_ordering lhs, std::weak_ordering rhs) noexcept
379 { return static_cast<std::weak_ordering>(lhs) == rhs; }
380
381 friend constexpr bool operator!=(weak_ordering lhs, std::weak_ordering rhs) noexcept
382 { return static_cast<std::weak_ordering>(lhs) != rhs; }
383
384 friend constexpr bool operator==(weak_ordering lhs, std::partial_ordering rhs) noexcept
385 { return static_cast<std::weak_ordering>(lhs) == rhs; }
386
387 friend constexpr bool operator!=(weak_ordering lhs, std::partial_ordering rhs) noexcept
388 { return static_cast<std::weak_ordering>(lhs) != rhs; }
389
390 friend constexpr bool operator==(weak_ordering lhs, std::strong_ordering rhs) noexcept
391 { return static_cast<std::weak_ordering>(lhs) == rhs; }
392
393 friend constexpr bool operator!=(weak_ordering lhs, std::strong_ordering rhs) noexcept
394 { return static_cast<std::weak_ordering>(lhs) != rhs; }
395
396 friend constexpr bool operator==(std::weak_ordering lhs, weak_ordering rhs) noexcept
397 { return lhs == static_cast<std::weak_ordering>(rhs); }
398
399 friend constexpr bool operator!=(std::weak_ordering lhs, weak_ordering rhs) noexcept
400 { return lhs != static_cast<std::weak_ordering>(rhs); }
401
402 friend constexpr bool operator==(std::partial_ordering lhs, weak_ordering rhs) noexcept
403 { return lhs == static_cast<std::weak_ordering>(rhs); }
404
405 friend constexpr bool operator!=(std::partial_ordering lhs, weak_ordering rhs) noexcept
406 { return lhs != static_cast<std::weak_ordering>(rhs); }
407
408 friend constexpr bool operator==(std::strong_ordering lhs, weak_ordering rhs) noexcept
409 { return lhs == static_cast<std::weak_ordering>(rhs); }
410
411 friend constexpr bool operator!=(std::strong_ordering lhs, weak_ordering rhs) noexcept
412 { return lhs != static_cast<std::weak_ordering>(rhs); }
413#endif // __cpp_lib_three_way_comparison
414
415private:
416 friend class strong_ordering;
417
418 constexpr explicit weak_ordering(QtPrivate::Ordering order) noexcept
419 : m_order(static_cast<QtPrivate::CompareUnderlyingType>(order))
420 {}
421
422 QT_WARNING_PUSH
423 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100903
424 QT_WARNING_DISABLE_GCC("-Wzero-as-null-pointer-constant")
425 QT_WARNING_DISABLE_CLANG("-Wzero-as-null-pointer-constant")
426 friend constexpr bool is_eq (weak_ordering o) noexcept { return o == 0; }
427 friend constexpr bool is_neq (weak_ordering o) noexcept { return o != 0; }
428 friend constexpr bool is_lt (weak_ordering o) noexcept { return o < 0; }
429 friend constexpr bool is_lteq(weak_ordering o) noexcept { return o <= 0; }
430 friend constexpr bool is_gt (weak_ordering o) noexcept { return o > 0; }
431 friend constexpr bool is_gteq(weak_ordering o) noexcept { return o >= 0; }
433
435};
436
440
442{
443public:
444 static const strong_ordering less;
446 static const strong_ordering equal;
448
449 constexpr Q_IMPLICIT operator partial_ordering() const noexcept
450 { return partial_ordering(static_cast<QtPrivate::Ordering>(m_order)); }
451
452 constexpr Q_IMPLICIT operator weak_ordering() const noexcept
453 { return weak_ordering(static_cast<QtPrivate::Ordering>(m_order)); }
454
455 friend constexpr bool operator==(strong_ordering lhs,
457 { return lhs.m_order == 0; }
458
459 friend constexpr bool operator!=(strong_ordering lhs,
461 { return lhs.m_order != 0; }
462
463 friend constexpr bool operator< (strong_ordering lhs,
465 { return lhs.m_order < 0; }
466
467 friend constexpr bool operator<=(strong_ordering lhs,
469 { return lhs.m_order <= 0; }
470
471 friend constexpr bool operator> (strong_ordering lhs,
473 { return lhs.m_order > 0; }
474
475 friend constexpr bool operator>=(strong_ordering lhs,
477 { return lhs.m_order >= 0; }
478
479
481 strong_ordering rhs) noexcept
482 { return 0 == rhs.m_order; }
483
485 strong_ordering rhs) noexcept
486 { return 0 != rhs.m_order; }
487
489 strong_ordering rhs) noexcept
490 { return 0 < rhs.m_order; }
491
493 strong_ordering rhs) noexcept
494 { return 0 <= rhs.m_order; }
495
497 strong_ordering rhs) noexcept
498 { return 0 > rhs.m_order; }
499
501 strong_ordering rhs) noexcept
502 { return 0 >= rhs.m_order; }
503
504
505#ifdef __cpp_lib_three_way_comparison
506 friend constexpr std::strong_ordering
508 { return lhs; } // https://eel.is/c++draft/cmp.strongord#6
509
510 friend constexpr std::strong_ordering
512 { return QtOrderingPrivate::reversed(rhs); }
513#endif // __cpp_lib_three_way_comparison
514
515
516 friend constexpr bool operator==(strong_ordering lhs, strong_ordering rhs) noexcept
517 { return lhs.m_order == rhs.m_order; }
518
519 friend constexpr bool operator!=(strong_ordering lhs, strong_ordering rhs) noexcept
520 { return lhs.m_order != rhs.m_order; }
521
522 friend constexpr bool operator==(strong_ordering lhs, partial_ordering rhs) noexcept
523 { return static_cast<partial_ordering>(lhs) == rhs; }
524
525 friend constexpr bool operator!=(strong_ordering lhs, partial_ordering rhs) noexcept
526 { return static_cast<partial_ordering>(lhs) == rhs; }
527
528 friend constexpr bool operator==(partial_ordering lhs, strong_ordering rhs) noexcept
529 { return lhs == static_cast<partial_ordering>(rhs); }
530
531 friend constexpr bool operator!=(partial_ordering lhs, strong_ordering rhs) noexcept
532 { return lhs != static_cast<partial_ordering>(rhs); }
533
534 friend constexpr bool operator==(strong_ordering lhs, weak_ordering rhs) noexcept
535 { return static_cast<weak_ordering>(lhs) == rhs; }
536
537 friend constexpr bool operator!=(strong_ordering lhs, weak_ordering rhs) noexcept
538 { return static_cast<weak_ordering>(lhs) == rhs; }
539
540 friend constexpr bool operator==(weak_ordering lhs, strong_ordering rhs) noexcept
541 { return lhs == static_cast<weak_ordering>(rhs); }
542
543 friend constexpr bool operator!=(weak_ordering lhs, strong_ordering rhs) noexcept
544 { return lhs != static_cast<weak_ordering>(rhs); }
545
546#ifdef __cpp_lib_three_way_comparison
548 {
553 else if (stdorder == std::strong_ordering::equal)
555 else if (stdorder == std::strong_ordering::greater)
557 }
558
559 constexpr Q_IMPLICIT operator std::strong_ordering() const noexcept
560 {
561 static_assert(sizeof(*this) == sizeof(std::strong_ordering));
562#ifdef __cpp_lib_bit_cast
563 return std::bit_cast<std::strong_ordering>(*this);
564#else
565 using O = QtPrivate::Ordering;
566 using R = std::strong_ordering;
567 switch (m_order) {
568 case qToUnderlying(O::Less): return R::less;
569 case qToUnderlying(O::Greater): return R::greater;
570 case qToUnderlying(O::Equal): return R::equal;
571 }
573#endif // __cpp_lib_bit_cast
574 }
575
576 friend constexpr bool operator==(strong_ordering lhs, std::strong_ordering rhs) noexcept
577 { return static_cast<std::strong_ordering>(lhs) == rhs; }
578
579 friend constexpr bool operator!=(strong_ordering lhs, std::strong_ordering rhs) noexcept
580 { return static_cast<std::strong_ordering>(lhs) != rhs; }
581
582 friend constexpr bool operator==(strong_ordering lhs, std::partial_ordering rhs) noexcept
583 { return static_cast<std::strong_ordering>(lhs) == rhs; }
584
585 friend constexpr bool operator!=(strong_ordering lhs, std::partial_ordering rhs) noexcept
586 { return static_cast<std::strong_ordering>(lhs) != rhs; }
587
588 friend constexpr bool operator==(strong_ordering lhs, std::weak_ordering rhs) noexcept
589 { return static_cast<std::strong_ordering>(lhs) == rhs; }
590
591 friend constexpr bool operator!=(strong_ordering lhs, std::weak_ordering rhs) noexcept
592 { return static_cast<std::strong_ordering>(lhs) != rhs; }
593
594 friend constexpr bool operator==(std::strong_ordering lhs, strong_ordering rhs) noexcept
595 { return lhs == static_cast<std::strong_ordering>(rhs); }
596
597 friend constexpr bool operator!=(std::strong_ordering lhs, strong_ordering rhs) noexcept
598 { return lhs != static_cast<std::strong_ordering>(rhs); }
599
600 friend constexpr bool operator==(std::partial_ordering lhs, strong_ordering rhs) noexcept
601 { return lhs == static_cast<std::strong_ordering>(rhs); }
602
603 friend constexpr bool operator!=(std::partial_ordering lhs, strong_ordering rhs) noexcept
604 { return lhs != static_cast<std::strong_ordering>(rhs); }
605
606 friend constexpr bool operator==(std::weak_ordering lhs, strong_ordering rhs) noexcept
607 { return lhs == static_cast<std::strong_ordering>(rhs); }
608
609 friend constexpr bool operator!=(std::weak_ordering lhs, strong_ordering rhs) noexcept
610 { return lhs != static_cast<std::strong_ordering>(rhs); }
611#endif // __cpp_lib_three_way_comparison
612
613 private:
614 constexpr explicit strong_ordering(QtPrivate::Ordering order) noexcept
615 : m_order(static_cast<QtPrivate::CompareUnderlyingType>(order))
616 {}
617
618 QT_WARNING_PUSH
619 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100903
620 QT_WARNING_DISABLE_GCC("-Wzero-as-null-pointer-constant")
621 QT_WARNING_DISABLE_CLANG("-Wzero-as-null-pointer-constant")
622 friend constexpr bool is_eq (strong_ordering o) noexcept { return o == 0; }
623 friend constexpr bool is_neq (strong_ordering o) noexcept { return o != 0; }
624 friend constexpr bool is_lt (strong_ordering o) noexcept { return o < 0; }
625 friend constexpr bool is_lteq(strong_ordering o) noexcept { return o <= 0; }
626 friend constexpr bool is_gt (strong_ordering o) noexcept { return o > 0; }
627 friend constexpr bool is_gteq(strong_ordering o) noexcept { return o >= 0; }
629
631};
632
637
638} // namespace Qt
639
640QT_BEGIN_INCLUDE_NAMESPACE
641
642// This is intentionally included after Qt::*_ordering types and before
643// qCompareThreeWay. Do not change!
644#include <QtCore/qcomparehelpers.h>
645
646QT_END_INCLUDE_NAMESPACE
647
648#if defined(Q_QDOC)
649
650template <typename LeftType, typename RightType>
651auto qCompareThreeWay(const LeftType &lhs, const RightType &rhs);
652
653#else
654
655template <typename LT, typename RT,
656 std::enable_if_t<
657 std::disjunction_v<
658 QtOrderingPrivate::CompareThreeWayTester::HasCompareThreeWay<LT, RT>,
659 QtOrderingPrivate::CompareThreeWayTester::HasCompareThreeWay<RT, LT>>,
660 bool> = true>
661auto qCompareThreeWay(const LT &lhs, const RT &rhs)
662 noexcept(QtOrderingPrivate::CompareThreeWayTester::compareThreeWayNoexcept<LT, RT>())
663{
664 using Qt::compareThreeWay;
665 if constexpr (QtOrderingPrivate::CompareThreeWayTester::hasCompareThreeWay_v<LT, RT>) {
666 return compareThreeWay(lhs, rhs);
667 } else {
668 const auto retval = compareThreeWay(rhs, lhs);
669 return QtOrderingPrivate::reversed(retval);
670 }
671}
672
673#endif // defined(Q_QDOC)
674
675//
676// Legacy QPartialOrdering
677//
678
679namespace QtPrivate {
681{
682 Unordered = QtPrivate::LegacyUncomparableValue
683};
684}
685
686// [cmp.partialord]
688{
689public:
690 static const QPartialOrdering Less;
694
695 static const QPartialOrdering less;
699
700 friend constexpr bool operator==(QPartialOrdering lhs,
702 { return lhs.isOrdered() && lhs.m_order == 0; }
703
704 friend constexpr bool operator!=(QPartialOrdering lhs,
706 { return !lhs.isOrdered() || lhs.m_order != 0; }
707
708 friend constexpr bool operator< (QPartialOrdering lhs,
710 { return lhs.isOrdered() && lhs.m_order < 0; }
711
712 friend constexpr bool operator<=(QPartialOrdering lhs,
714 { return lhs.isOrdered() && lhs.m_order <= 0; }
715
716 friend constexpr bool operator> (QPartialOrdering lhs,
718 { return lhs.isOrdered() && lhs.m_order > 0; }
719
720 friend constexpr bool operator>=(QPartialOrdering lhs,
722 { return lhs.isOrdered() && lhs.m_order >= 0; }
723
724
726 QPartialOrdering rhs) noexcept
727 { return rhs.isOrdered() && 0 == rhs.m_order; }
728
730 QPartialOrdering rhs) noexcept
731 { return !rhs.isOrdered() || 0 != rhs.m_order; }
732
734 QPartialOrdering rhs) noexcept
735 { return rhs.isOrdered() && 0 < rhs.m_order; }
736
738 QPartialOrdering rhs) noexcept
739 { return rhs.isOrdered() && 0 <= rhs.m_order; }
740
742 QPartialOrdering rhs) noexcept
743 { return rhs.isOrdered() && 0 > rhs.m_order; }
744
746 QPartialOrdering rhs) noexcept
747 { return rhs.isOrdered() && 0 >= rhs.m_order; }
748
749
750#ifdef __cpp_lib_three_way_comparison
751 friend constexpr std::partial_ordering
753 { return lhs; } // https://eel.is/c++draft/cmp.partialord#4
754
755 friend constexpr std::partial_ordering
757 { return QtOrderingPrivate::reversed(rhs); }
758#endif // __cpp_lib_three_way_comparison
759
760
761 friend constexpr bool operator==(QPartialOrdering lhs, QPartialOrdering rhs) noexcept
762 { return lhs.m_order == rhs.m_order; }
763
764 friend constexpr bool operator!=(QPartialOrdering lhs, QPartialOrdering rhs) noexcept
765 { return lhs.m_order != rhs.m_order; }
766
777
780
783
784 constexpr Q_IMPLICIT operator Qt::partial_ordering() const noexcept
785 {
786 using O = QtPrivate::Ordering;
788 using R = Qt::partial_ordering;
789 switch (m_order) {
790 case qToUnderlying(O::Less): return R::less;
791 case qToUnderlying(O::Greater): return R::greater;
792 case qToUnderlying(O::Equivalent): return R::equivalent;
793 case qToUnderlying(U::Unordered): return R::unordered;
794 }
795 // GCC 8.x does not treat __builtin_unreachable() as constexpr
796#if !defined(Q_CC_GNU_ONLY) || (Q_CC_GNU >= 900)
797 // NOLINTNEXTLINE(qt-use-unreachable-return): Triggers on Clang, breaking GCC 8
799#endif
800 return R::unordered;
801 }
802
803 friend constexpr bool operator==(QPartialOrdering lhs, Qt::partial_ordering rhs) noexcept
804 { Qt::partial_ordering qt = lhs; return qt == rhs; }
805
806 friend constexpr bool operator!=(QPartialOrdering lhs, Qt::partial_ordering rhs) noexcept
807 { Qt::partial_ordering qt = lhs; return qt != rhs; }
808
809 friend constexpr bool operator==(Qt::partial_ordering lhs, QPartialOrdering rhs) noexcept
810 { Qt::partial_ordering qt = rhs; return lhs == qt; }
811
812 friend constexpr bool operator!=(Qt::partial_ordering lhs, QPartialOrdering rhs) noexcept
813 { Qt::partial_ordering qt = rhs; return lhs != qt; }
814
815#ifdef __cpp_lib_three_way_comparison
817 {
822 else if (stdorder == std::partial_ordering::greater)
826 }
827
830
833
834 constexpr Q_IMPLICIT operator std::partial_ordering() const noexcept
835 {
836 using O = QtPrivate::Ordering;
838 using R = std::partial_ordering;
839 switch (m_order) {
840 case qToUnderlying(O::Less): return R::less;
841 case qToUnderlying(O::Greater): return R::greater;
842 case qToUnderlying(O::Equivalent): return R::equivalent;
843 case qToUnderlying(U::Unordered): return R::unordered;
844 }
846 }
847
848 friend constexpr bool operator==(QPartialOrdering lhs, std::partial_ordering rhs) noexcept
849 { return static_cast<std::partial_ordering>(lhs) == rhs; }
850
851 friend constexpr bool operator!=(QPartialOrdering lhs, std::partial_ordering rhs) noexcept
852 { return static_cast<std::partial_ordering>(lhs) != rhs; }
853
854 friend constexpr bool operator==(std::partial_ordering lhs, QPartialOrdering rhs) noexcept
855 { return lhs == static_cast<std::partial_ordering>(rhs); }
856
857 friend constexpr bool operator!=(std::partial_ordering lhs, QPartialOrdering rhs) noexcept
858 { return lhs != static_cast<std::partial_ordering>(rhs); }
859#endif // __cpp_lib_three_way_comparison
860
861private:
862 constexpr explicit QPartialOrdering(QtPrivate::Ordering order) noexcept
863 : m_order(static_cast<QtPrivate::CompareUnderlyingType>(order))
864 {}
865 constexpr explicit QPartialOrdering(QtPrivate::LegacyUncomparable order) noexcept
866 : m_order(static_cast<QtPrivate::CompareUnderlyingType>(order))
867 {}
868
869 QT_WARNING_PUSH
870 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100903
871 QT_WARNING_DISABLE_GCC("-Wzero-as-null-pointer-constant")
872 QT_WARNING_DISABLE_CLANG("-Wzero-as-null-pointer-constant")
873 friend constexpr bool is_eq (QPartialOrdering o) noexcept { return o == 0; }
874 friend constexpr bool is_neq (QPartialOrdering o) noexcept { return o != 0; }
875 friend constexpr bool is_lt (QPartialOrdering o) noexcept { return o < 0; }
876 friend constexpr bool is_lteq(QPartialOrdering o) noexcept { return o <= 0; }
877 friend constexpr bool is_gt (QPartialOrdering o) noexcept { return o > 0; }
878 friend constexpr bool is_gteq(QPartialOrdering o) noexcept { return o >= 0; }
880
881 // instead of the exposition only is_ordered member in [cmp.partialord],
882 // use a private function
883 constexpr bool isOrdered() const noexcept
885
887};
888
893
898
899QT_END_NAMESPACE
900
901#endif // QCOMPARE_H
\variable Qt::partial_ordering::less
Definition qcompare.h:688
friend constexpr bool operator==(QPartialOrdering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:700
static const QPartialOrdering unordered
Definition qcompare.h:698
friend constexpr bool operator==(QPartialOrdering lhs, Qt::partial_ordering rhs) noexcept
Definition qcompare.h:803
static const QPartialOrdering greater
Definition qcompare.h:697
friend constexpr bool operator<(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering rhs) noexcept
Definition qcompare.h:733
friend constexpr bool operator<(QPartialOrdering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:708
static const QPartialOrdering less
Definition qcompare.h:695
friend constexpr bool operator!=(QPartialOrdering lhs, Qt::partial_ordering rhs) noexcept
Definition qcompare.h:806
friend constexpr bool operator==(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering rhs) noexcept
Definition qcompare.h:725
friend constexpr bool operator==(Qt::partial_ordering lhs, QPartialOrdering rhs) noexcept
Definition qcompare.h:809
friend constexpr bool operator>(QPartialOrdering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:716
static const QPartialOrdering Less
Definition qcompare.h:690
friend constexpr bool operator>=(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering rhs) noexcept
Definition qcompare.h:745
friend constexpr bool operator==(QPartialOrdering lhs, QPartialOrdering rhs) noexcept
Return true if lhs and rhs represent the same result; otherwise, returns false.
Definition qcompare.h:761
static const QPartialOrdering equivalent
Definition qcompare.h:696
friend constexpr bool operator!=(Qt::partial_ordering lhs, QPartialOrdering rhs) noexcept
Definition qcompare.h:812
friend constexpr bool operator!=(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering rhs) noexcept
Definition qcompare.h:729
friend constexpr bool operator<=(QPartialOrdering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:712
friend constexpr bool operator!=(QPartialOrdering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:704
friend constexpr bool operator!=(QPartialOrdering lhs, QPartialOrdering rhs) noexcept
Return true if lhs and rhs represent different results; otherwise, returns true.
Definition qcompare.h:764
friend constexpr bool operator>(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering rhs) noexcept
Definition qcompare.h:741
static const QPartialOrdering Greater
Definition qcompare.h:692
friend constexpr bool operator<=(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering rhs) noexcept
Definition qcompare.h:737
static const QPartialOrdering Equivalent
Definition qcompare.h:691
static const QPartialOrdering Unordered
Definition qcompare.h:693
friend constexpr bool operator>=(QPartialOrdering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:720
\variable Qt::weak_ordering::less
Definition qcompare.h:82
friend constexpr bool operator>=(QtPrivate::CompareAgainstLiteralZero, partial_ordering rhs) noexcept
Definition qcompare.h:134
friend constexpr bool operator>(partial_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:105
friend constexpr bool operator>(QtPrivate::CompareAgainstLiteralZero, partial_ordering rhs) noexcept
Definition qcompare.h:130
friend constexpr bool operator==(QtPrivate::CompareAgainstLiteralZero, partial_ordering rhs) noexcept
Definition qcompare.h:114
friend constexpr bool operator<=(partial_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:101
static const partial_ordering equivalent
Definition qcompare.h:85
friend constexpr bool operator<=(QtPrivate::CompareAgainstLiteralZero, partial_ordering rhs) noexcept
Definition qcompare.h:126
friend constexpr bool operator==(partial_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:89
friend constexpr bool operator!=(QtPrivate::CompareAgainstLiteralZero, partial_ordering rhs) noexcept
Definition qcompare.h:118
friend constexpr bool operator!=(partial_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:93
friend constexpr bool operator<(partial_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:97
friend constexpr bool operator<(QtPrivate::CompareAgainstLiteralZero, partial_ordering rhs) noexcept
Definition qcompare.h:122
static const partial_ordering unordered
Definition qcompare.h:87
friend constexpr bool operator>=(partial_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:109
static const partial_ordering greater
Definition qcompare.h:86
friend constexpr bool operator!=(partial_ordering lhs, partial_ordering rhs) noexcept
Return true if lhs and rhs represent different results; otherwise, returns true.
Definition qcompare.h:153
static const partial_ordering less
Definition qcompare.h:84
friend constexpr bool operator==(partial_ordering lhs, partial_ordering rhs) noexcept
Return true if lhs and rhs represent the same result; otherwise, returns false.
Definition qcompare.h:150
\inmodule QtCore \title Classes and helpers for defining comparison operators \keyword qtcompare
Definition qcompare.h:442
friend constexpr bool operator<=(strong_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:467
friend constexpr bool operator!=(strong_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:459
friend constexpr bool operator<(strong_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:463
friend constexpr bool operator<(QtPrivate::CompareAgainstLiteralZero, strong_ordering rhs) noexcept
Definition qcompare.h:488
static const strong_ordering greater
Definition qcompare.h:447
friend constexpr bool operator==(strong_ordering lhs, partial_ordering rhs) noexcept
Definition qcompare.h:522
friend constexpr bool operator>(strong_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:471
friend constexpr bool operator!=(weak_ordering lhs, strong_ordering rhs) noexcept
Definition qcompare.h:543
friend constexpr bool operator!=(partial_ordering lhs, strong_ordering rhs) noexcept
Definition qcompare.h:531
static const strong_ordering equal
Definition qcompare.h:446
friend constexpr bool operator!=(QtPrivate::CompareAgainstLiteralZero, strong_ordering rhs) noexcept
Definition qcompare.h:484
friend constexpr bool operator==(QtPrivate::CompareAgainstLiteralZero, strong_ordering rhs) noexcept
Definition qcompare.h:480
friend constexpr bool operator==(strong_ordering lhs, weak_ordering rhs) noexcept
Definition qcompare.h:534
friend constexpr bool operator>=(QtPrivate::CompareAgainstLiteralZero, strong_ordering rhs) noexcept
Definition qcompare.h:500
static const strong_ordering less
Definition qcompare.h:444
friend constexpr bool operator!=(strong_ordering lhs, weak_ordering rhs) noexcept
Definition qcompare.h:537
friend constexpr bool operator!=(strong_ordering lhs, partial_ordering rhs) noexcept
Definition qcompare.h:525
friend constexpr bool operator==(strong_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:455
static const strong_ordering equivalent
Definition qcompare.h:445
friend constexpr bool operator>(QtPrivate::CompareAgainstLiteralZero, strong_ordering rhs) noexcept
Definition qcompare.h:496
friend constexpr bool operator<=(QtPrivate::CompareAgainstLiteralZero, strong_ordering rhs) noexcept
Definition qcompare.h:492
friend constexpr bool operator==(strong_ordering lhs, strong_ordering rhs) noexcept
Returns true if lhs and rhs represent the same result; otherwise, returns false.
Definition qcompare.h:516
friend constexpr bool operator!=(strong_ordering lhs, strong_ordering rhs) noexcept
Returns true if lhs and rhs represent different results; otherwise, returns true.
Definition qcompare.h:519
friend constexpr bool operator==(weak_ordering lhs, strong_ordering rhs) noexcept
Definition qcompare.h:540
friend constexpr bool operator==(partial_ordering lhs, strong_ordering rhs) noexcept
Definition qcompare.h:528
friend constexpr bool operator>=(strong_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:475
\variable Qt::strong_ordering::less
Definition qcompare.h:262
friend constexpr bool operator!=(weak_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:275
static const weak_ordering less
Definition qcompare.h:264
friend constexpr bool operator<=(weak_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:283
friend constexpr bool operator<(weak_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:279
friend constexpr bool operator==(weak_ordering lhs, partial_ordering rhs) noexcept
Definition qcompare.h:338
friend constexpr bool operator<(QtPrivate::CompareAgainstLiteralZero, weak_ordering rhs) noexcept
Definition qcompare.h:304
friend constexpr bool operator>(QtPrivate::CompareAgainstLiteralZero, weak_ordering rhs) noexcept
Definition qcompare.h:312
friend constexpr bool operator==(weak_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:271
friend constexpr bool operator!=(QtPrivate::CompareAgainstLiteralZero, weak_ordering rhs) noexcept
Definition qcompare.h:300
friend constexpr bool operator>=(weak_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:291
friend constexpr bool operator==(weak_ordering lhs, weak_ordering rhs) noexcept
Return true if lhs and rhs represent the same result; otherwise, returns false.
Definition qcompare.h:332
friend constexpr bool operator>=(QtPrivate::CompareAgainstLiteralZero, weak_ordering rhs) noexcept
Definition qcompare.h:316
static const weak_ordering greater
Definition qcompare.h:266
friend constexpr bool operator>(weak_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept
Definition qcompare.h:287
friend constexpr bool operator!=(weak_ordering lhs, weak_ordering rhs) noexcept
Return true if lhs and rhs represent different results; otherwise, returns true.
Definition qcompare.h:335
friend constexpr bool operator!=(weak_ordering lhs, partial_ordering rhs) noexcept
Definition qcompare.h:341
friend constexpr bool operator<=(QtPrivate::CompareAgainstLiteralZero, weak_ordering rhs) noexcept
Definition qcompare.h:308
friend constexpr bool operator!=(partial_ordering lhs, weak_ordering rhs) noexcept
Definition qcompare.h:347
friend constexpr bool operator==(partial_ordering lhs, weak_ordering rhs) noexcept
Definition qcompare.h:344
static const weak_ordering equivalent
Definition qcompare.h:265
friend constexpr bool operator==(QtPrivate::CompareAgainstLiteralZero, weak_ordering rhs) noexcept
Definition qcompare.h:296
Combined button and popup list for selecting options.
constexpr O reversed(O o) noexcept
Definition qcompare.h:66
constexpr CompareUnderlyingType LegacyUncomparableValue
Definition qcompare.h:27
LegacyUncomparable
Definition qcompare.h:681
Definition qcompare.h:76