diff options
| author | Ivan Solovev <ivan.solovev@qt.io> | 2023-05-17 15:01:58 +0200 |
|---|---|---|
| committer | Ivan Solovev <ivan.solovev@qt.io> | 2023-11-28 21:30:33 +0100 |
| commit | 96f494bf92d246b741b6ae6261c93ef557ef392b (patch) | |
| tree | df95a915535a9acc26166c45a21006a76f996192 /src/corelib/global/qcompare.cpp | |
| parent | 5a28aacd850da6ba4a4e4cd3067f60d0f82dd8c0 (diff) | |
Implement helper Qt::compareThreeWay() function for built-in types
The helper function
RetType compareThreeWay(const T &left, const T &right) noexcept;
is used for C++20-comparison macros. Normally it's the user's
responsibility to provide this function as a hidden friend of the class
which uses the comparison helper macros.
For built-in types we provide the implementation inside the Qt
namespace.
We have to use custom IsIntegralType trait because libstdc++ only
treats __{u}int128_t types as integral when compiling in -std=gnu++XX
mode, and we compile Qt in -std=c++XX mode.
This patch provides the implementations only for compareThreeWay()
overloads, because there is no need to implement comparesEqual() for
built-in types. It would just be equivalent to calling operator==(),
so the user can do it directly.
Task-number: QTBUG-104113
Change-Id: I7b3f395458e1ee4c64f442ad48bbf4fec4c19c52
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/global/qcompare.cpp')
| -rw-r--r-- | src/corelib/global/qcompare.cpp | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/corelib/global/qcompare.cpp b/src/corelib/global/qcompare.cpp index 9e4d958d167..9d1273c4e6a 100644 --- a/src/corelib/global/qcompare.cpp +++ b/src/corelib/global/qcompare.cpp @@ -1098,4 +1098,155 @@ CHECK(strong, equivalent); Q_DECLARE_EQUALITY_COMPARABLE */ +/*! + \fn template <typename LeftInt, typename RightInt> Qt::compareThreeWay(LeftInt lhs, RightInt rhs) + \since 6.7 + \relates <QtCompare> + \overload + + Implements three-way comparison of integral types. + + \note This function participates in overload resolution only if both + \c LeftInt and \c RightInt are built-in integral types. + + Returns \c {lhs <=> rhs}, provided \c LeftInt and \c RightInt are built-in + integral types. Unlike \c {operator<=>()}, this function template is also + available in C++17. See + \l {https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison} + {cppreference} for more details. + + This function can also be used in custom \c {compareThreeWay()} functions, + when ordering members of a custom class represented by built-in types: + + \code + class MyClass { + public: + ... + private: + int value; + ... + friend Qt::strong_ordering + compareThreeWay(const MyClass &lhs, const MyClass &rhs) noexcept + { return Qt::compareThreeWay(lhs.value, rhs.value); } + Q_DECLARE_STRONGLY_ORDERED(MyClass) + }; + \endcode + + Returns an instance of \l Qt::strong_ordering that represents the relation + between \a lhs and \a rhs. +*/ + +/*! + \fn template <typename LeftFloat, typename RightFloat> Qt::compareThreeWay(LeftFloat lhs, RightFloat rhs) + \since 6.7 + \relates <QtCompare> + \overload + + Implements three-way comparison of floating point types. + + \note This function participates in overload resolution only if both + \c LeftFloat and \c RightFloat are built-in floating-point types. + + Returns \c {lhs <=> rhs}, provided \c LeftFloat and \c RightFloat are + built-in floating-point types. Unlike \c {operator<=>()}, this function + template is also available in C++17. See + \l {https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison} + {cppreference} for more details. + + This function can also be used in custom \c {compareThreeWay()} functions, + when ordering members of a custom class represented by built-in types: + + \code + class MyClass { + public: + ... + private: + double value; + ... + friend Qt::partial_ordering + compareThreeWay(const MyClass &lhs, const MyClass &rhs) noexcept + { return Qt::compareThreeWay(lhs.value, rhs.value); } + Q_DECLARE_PARTIALLY_ORDERED(MyClass) + }; + \endcode + + Returns an instance of \l Qt::partial_ordering that represents the relation + between \a lhs and \a rhs. If \a lhs or \a rhs is not a number (NaN), + \l Qt::partial_ordering::unordered is returned. +*/ + +/*! + \fn template <typename IntType, typename FloatType> Qt::compareThreeWay(IntType lhs, FloatType rhs) + \since 6.7 + \relates <QtCompare> + \overload + + Implements three-way comparison of integral and floating point types. + + \note This function participates in overload resolution only if \c IntType + is a built-in integral type and \c FloatType is a built-in floating-point + type. + + This function converts \a lhs to \c FloatType and calls the overload for + floating-point types. + + Returns an instance of \l Qt::partial_ordering that represents the relation + between \a lhs and \a rhs. If \a rhs is not a number (NaN), + \l Qt::partial_ordering::unordered is returned. +*/ + +/*! + \fn template <typename FloatType, typename IntType> Qt::compareThreeWay(FloatType lhs, IntType rhs) + \since 6.7 + \relates <QtCompare> + \overload + + Implements three-way comparison of floating point and integral types. + + \note This function participates in overload resolution only if \c FloatType + is a built-in floating-point type and \c IntType is a built-in integral + type. + + This function converts \a rhs to \c FloatType and calls the overload for + floating-point types. + + Returns an instance of \l Qt::partial_ordering that represents the relation + between \a lhs and \a rhs. If \a lhs is not a number (NaN), + \l Qt::partial_ordering::unordered is returned. +*/ + +/*! + \fn template <typename LeftType, typename RightType> Qt::compareThreeWay(const LeftType *lhs, const RightType *rhs) + \since 6.7 + \relates <QtCompare> + \overload + + Implements three-way comparison of pointers. + + \note This function participates in overload resolution if \c LeftType and + \c RightType are the same type, or base and derived types. It is also used + to compare any pointer to \c {std::nullptr_t}. + + Returns an instance of \l Qt::strong_ordering that represents the relation + between \a lhs and \a rhs. +*/ + +/*! + \fn template <class Enum> Qt::compareThreeWay(Enum lhs, Enum rhs) + \since 6.7 + \relates <QtCompare> + \overload + + Implements three-way comparison of enum types. + + \note This function participates in overload resolution only if \c Enum + is an enum type. + + This function converts \c Enum to its underlying type and calls the + overload for integral types. + + Returns an instance of \l Qt::strong_ordering that represents the relation + between \a lhs and \a rhs. +*/ + QT_END_NAMESPACE |
