<QtCompare> - Classes and helpers for defining comparison operators
<QtCompare> 头文件定义了Qt::*_ordering 类型以及用于定义比较运算符的辅助宏。更多内容...
| Header: | #include <QtCompare> |
类型
(since 6.12) | if_has_qt_compare_three_way |
函数
(since 6.7) Qt::strong_ordering | compareThreeWay(Enum lhs, Enum rhs) |
(since 6.7) auto | compareThreeWay(FloatType lhs, IntType rhs) |
(since 6.7) auto | compareThreeWay(IntType lhs, FloatType rhs) |
(since 6.7) auto | compareThreeWay(LeftFloat lhs, RightFloat rhs) |
(since 6.7) auto | compareThreeWay(LeftInt lhs, RightInt rhs) |
(since 6.8) Qt::strong_ordering | compareThreeWay(Qt::totally_ordered_wrapper<T *> lhs, Qt::totally_ordered_wrapper<U *> rhs) |
(since 6.8) Qt::strong_ordering | compareThreeWay(Qt::totally_ordered_wrapper<T *> lhs, U *rhs) |
(since 6.8) Qt::strong_ordering | compareThreeWay(Qt::totally_ordered_wrapper<T *> lhs, std::nullptr_t rhs) |
(since 6.8) Qt::strong_ordering | compareThreeWay(U *lhs, Qt::totally_ordered_wrapper<T *> rhs) |
(since 6.12) auto | compareThreeWay(const std::pair<T1, T2> &lhs, const std::pair<U1, U2> &rhs) |
(since 6.8) Qt::strong_ordering | compareThreeWay(std::nullptr_t lhs, Qt::totally_ordered_wrapper<T *> rhs) |
(since 6.7) auto | qCompareThreeWay(const LeftType &lhs, const RightType &rhs) |
详细说明
该头文件引入了Qt::partial_ordering 、Qt::weak_ordering 和Qt::strong_ordering 类型,它们是 Qt 对std::*_ordering 类型的 C++17 回溯实现。
该头文件还包含用于在 C++17 中实现三元比较的函数。
Qt::compareThreeWay() 函数的重载为 C++ 内置类型提供了三元比较功能。
qCompareThreeWay() 模板作为通用的三元比较实现。其实现依赖于Qt::compareThreeWay() 以及自由的compareThreeWay() 函数。
类型文档
[alias, since 6.12] template <typename T, typename U> if_has_qt_compare_three_way
std::enable_if_t 的一个便捷别名,可用于将函数模板限制为支持通过compareThreeWay() 进行三元比较的类型。
该 typedef 于 Qt 6.12 中引入。
另请参阅 Qt::has_qt_compare_three_way 。
函数文档
[constexpr noexcept, since 6.7] template <typename Enum> requires if_enum<Enum> Qt::strong_ordering compareThreeWay(Enum lhs, Enum rhs)
实现枚举类型的三路比较。
该函数将Enum 转换为其底层类型,并调用针对整数类型的重载方法。
返回一个Qt::strong_ordering 实例,该实例表示lhs 与rhs 之间的关系。
约束
仅当 `Enum ` 是枚举类型时,才参与重载解析。
这是一个重载函数。
该函数于 Qt 6.7 中引入。
[constexpr noexcept, since 6.7] template <typename FloatType, typename IntType> requires if_floating_point<FloatType> && if_integral<IntType> auto compareThreeWay(FloatType lhs, IntType rhs)
实现浮点型和整数型的三元比较。
该函数将rhs 转换为FloatType ,并调用浮点类型的重载方法。
返回一个Qt::partial_ordering 实例,该实例表示lhs 与rhs 之间的关系。如果lhs 不是数值(NaN),则返回Qt::partial_ordering::unordered 。
约束
仅当FloatType 是内置浮点类型且IntType 是内置整数类型时,才参与重载解析。
这是一个重载函数。
该函数在 Qt 6.7 中引入。
[constexpr noexcept, since 6.7] template <typename IntType, typename FloatType> requires if_integral<IntType> && if_floating_point<FloatType> auto compareThreeWay(IntType lhs, FloatType rhs)
实现整数和浮点数类型的三路比较。
该函数将lhs 转换为FloatType ,并调用针对浮点类型的重载函数。
返回一个Qt::partial_ordering 实例,该实例表示lhs 与rhs 之间的关系。如果rhs 不是数值(NaN),则返回Qt::partial_ordering::unordered 。
约束
仅当IntType 是内置整数类型且FloatType 是内置浮点类型时,才参与重载解析。
这是一个重载函数。
该函数在 Qt 6.7 中引入。
[constexpr noexcept, since 6.7] template <typename LeftFloat, typename RightFloat> requires if_floating_point<LeftFloat> && if_floating_point<RightFloat> auto compareThreeWay(LeftFloat lhs, RightFloat rhs)
实现浮点数类型的三路比较。
若LeftFloat 和RightFloat 均为内置浮点类型,则返回lhs <=> rhs 。与operator<=>() 不同,该函数模板在C++17中同样可用。更多详情请参见cppreference。
当对由内置类型表示的自定义类的成员进行排序时,此函数也可用于自定义的 `compareThreeWay() ` 函数中:
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)
};返回一个Qt::partial_ordering 实例,该实例表示lhs 与rhs 之间的关系。如果lhs 或rhs 不是数值(NaN),则返回Qt::partial_ordering::unordered 。
约束
仅当LeftFloat 和RightFloat 均为内置浮点类型时,才参与重载解析。
这是一个重载函数。
该函数在 Qt 6.7 中引入。
[constexpr noexcept, since 6.7] template <typename LeftInt, typename RightInt> requires if_integral<LeftInt> && if_integral<RightInt> auto compareThreeWay(LeftInt lhs, RightInt rhs)
实现整数类型的三路比较。
若LeftInt 和RightInt 均为内置整数类型,则返回lhs <=> rhs 。与operator<=>() 不同,该函数模板在C++17中同样可用。更多详情请参阅cppreference。
当对由内置类型表示的自定义类成员进行排序时,此函数也可用于自定义的compareThreeWay() 函数中:
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)
};返回一个Qt::strong_ordering 实例,该实例表示lhs 与rhs 之间的关系。
约束
仅当 `LeftInt ` 和 `RightInt ` 均为内置整数类型时,才参与重载解析。
这是一个重载函数。
该函数在 Qt 6.7 中引入。
[constexpr noexcept, since 6.8] template <typename T, typename U> requires if_compatible_pointers<T, U> Qt::strong_ordering compareThreeWay(Qt::totally_ordered_wrapper<T *> lhs, Qt::totally_ordered_wrapper<U *> rhs)
实现对封装在Qt::totally_ordered_wrapper 中的指针的三路比较。在进行比较时,采用指针之间的严格全序关系。
返回一个Qt::strong_ordering 的实例,该实例表示lhs 与rhs 之间的关系。
约束
仅当T 和U 属于同一类型,或为基类与派生类时,才参与重载解析。
这是一个重载函数。
该函数于 Qt 6.8 中引入。
[constexpr noexcept, since 6.8] template <typename T, typename U> requires if_compatible_pointers<T, U> Qt::strong_ordering compareThreeWay(Qt::totally_ordered_wrapper<T *> lhs, U *rhs)
实现对包装在Qt::totally_ordered_wrapper 中的指针与普通指针的三方比较。在进行比较时,采用指针间的严格全序。
返回一个Qt::strong_ordering 实例,该实例表示lhs 与rhs 之间的关系。
约束
仅当 `T ` 和 `U ` 类型相同,或为基类与派生类时,才参与重载解析。
这是一个重载函数。
该函数在 Qt 6.8 中引入。
[constexpr noexcept, since 6.8] template <typename T> Qt::strong_ordering compareThreeWay(Qt::totally_ordered_wrapper<T *> lhs, std::nullptr_t rhs)
实现对包装在Qt::totally_ordered_wrapper 中的指针与std::nullptr_t 之间的三路比较。
返回一个Qt::strong_ordering 实例,该实例表示lhs 与rhs 之间的关系。
这是一个重载函数。
该函数在 Qt 6.8 中引入。
[constexpr noexcept, since 6.8] template <typename T, typename U> requires if_compatible_pointers<T, U> Qt::strong_ordering compareThreeWay(U *lhs, Qt::totally_ordered_wrapper<T *> rhs)
实现普通指针与封装在Qt::totally_ordered_wrapper 中的指针之间的三方比较。在进行比较时,采用指针之间的严格全序。
返回一个Qt::strong_ordering 的实例,该实例表示lhs 与rhs 之间的关系。
约束
仅当 `T ` 和 `U ` 为同一类型,或为基类与派生类时,才参与重载解析。
这是一个重载函数。
该函数在 Qt 6.8 中引入。
[since 6.12] template <
typename T1,
typename T2,
typename U1,
typename U2
>
auto compareThreeWay(const std::pair<T1, T2> &lhs, const std::pair<U1, U2> &rhs)
注意: T1 应与U1 可比,而T2 应与U2 可比,具体可使用compareThreeWay() 或operator<() 进行验证。
此重载使用auto 作为返回类型,因为排序的强度取决于该对的模板参数。例如,如果任一参数是浮点类型,则排序关系为Qt::partial_ordering ;如果两者均为整数类型,则为Qt::strong_ordering 。
这是一个重载函数。
该函数在 Qt 6.12 中引入。
[constexpr noexcept, since 6.8] template <typename T> Qt::strong_ordering compareThreeWay(std::nullptr_t lhs, Qt::totally_ordered_wrapper<T *> rhs)
实现std::nullptr_t 与被包装在Qt::totally_ordered_wrapper 中的指针之间的三元比较。
返回一个Qt::strong_ordering 实例,该实例表示lhs 与rhs 之间的关系。
这是一个重载函数。
该函数在 Qt 6.8 中引入。
[since 6.7] template <typename LeftType, typename RightType> auto qCompareThreeWay(const LeftType &lhs, const RightType &rhs)
对lhs 和rhs 执行三元比较,并返回其中一种 Qt 排序类型作为结果。该函数在 C++17 和 C++20 中均可用。
实际返回的类型取决于 `LeftType ` 和 `RightType`。
注意: 仅当针对(LeftType, RightType) 这一对或其反向组合(RightType, LeftType) 实现了compareThreeWay() 时,此 函数模板才可用。
该方法等同于
using Qt::compareThreeWay;
return compareThreeWay(lhs, rhs);其中Qt::compareThreeWay 是 Qt 对内置类型的三路比较的实现。
自由的 `compareThreeWay ` 函数应为自定义类型提供三路比较。这些函数应返回 Qt 排序类型之一。
Qt 为其部分类型提供了compareThreeWay 实现。
注意: 请勿为 Qt 类型重新实现compareThreeWay() ,因为在未来的 Qt 版本中,将有更多 Qt 类型支持此功能。
主要在通用代码中使用此函数,即当你对 `LeftType ` 和 `RightType` 一无所知时。
若已知具体类型,请使用
Qt::compareThreeWay针对内置类型compareThreeWay针对自定义类型
仅在仅使用 C++20 或更高版本编译的代码中直接使用 `operator<=>() `。
该函数在 Qt 6.7 中引入。
另请参阅 Qt::partial_ordering 、Qt::weak_ordering 以及Qt::strong_ordering 。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.