![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
\inmodule QtCore \inheaderfile QtCompare More...
#include <qcomparehelpers.h>
Public Member Functions | |
| totally_ordered_wrapper () noexcept=default | |
| Q_IMPLICIT constexpr | totally_ordered_wrapper (std::nullptr_t) |
| constexpr | totally_ordered_wrapper (P p) noexcept |
| constexpr P | get () const noexcept |
| constexpr void | reset (P p) noexcept |
| constexpr P | operator-> () const noexcept |
| template<typename U = T, std::enable_if_t<!std::is_void_v< U >, bool > = true> | |
| constexpr U & | operator* () const noexcept |
| constexpr | operator bool () const noexcept |
Friends | |
| void | qt_ptr_swap (totally_ordered_wrapper &lhs, totally_ordered_wrapper &rhs) noexcept |
| void | swap (totally_ordered_wrapper &lhs, totally_ordered_wrapper &rhs) noexcept |
| size_t | qHash (totally_ordered_wrapper key, size_t seed=0) noexcept |
\inmodule QtCore \inheaderfile QtCompare
Qt::totally_ordered_wrapper is a wrapper type that provides strict total order for the wrapped types.
One of its primary usecases is to prevent {Undefined Behavior} (UB) when comparing pointers.
Consider the following simple class:
Lexicographical comparison of the two instances of the PointerWrapperBad type would result in UB, because it will call {operator<()} or {operator<=>()} on the {ptr} members.
To fix it, use the new wrapper type:
The {operator<()} and (if available) {operator<=>()} operators for the {Qt::totally_ordered_wrapper} type use the \l {https://en.cppreference.com/w/cpp/utility/functional/less}{std::less} and \l {https://en.cppreference.com/w/cpp/utility/compare/compare_three_way} {std::compare_three_way} function objects respectively, providing \l {https://en.cppreference.com/w/cpp/language/operator_comparison#Pointer_total_order} {strict total order over pointers} when doing the comparison.
As a result, the relational operators for {PointerWrapperGood::ptr} member will be well-defined, and we can even {=default} the relational operators for the {PointerWrapperGood} class, like it's shown above.
Definition at line 1087 of file qcomparehelpers.h.
|
defaultnoexcept |
|
inlineconstexpr |
Definition at line 1095 of file qcomparehelpers.h.
|
inlineexplicitconstexprnoexcept |
Definition at line 1098 of file qcomparehelpers.h.
|
inlineconstexprnoexcept |
Definition at line 1100 of file qcomparehelpers.h.
Referenced by operator bool(), operator*(), and operator->().
|
inlineexplicitconstexprnoexcept |
Definition at line 1106 of file qcomparehelpers.h.
References get().
|
inlineconstexprnoexcept |
Definition at line 1104 of file qcomparehelpers.h.
References get().
|
inlineconstexprnoexcept |
Definition at line 1102 of file qcomparehelpers.h.
References get().
Definition at line 1101 of file qcomparehelpers.h.
|
friend |
Definition at line 1148 of file qcomparehelpers.h.
|
friend |
Definition at line 1144 of file qcomparehelpers.h.
|
friend |
Definition at line 1146 of file qcomparehelpers.h.