<QtCompare> - Classes and helpers for defining comparison operators
<QtCompare> 头文件定义了Qt::*_ordering
类型和用于定义比较运算符的辅助宏。更多
Header: | #include <QtCompare> |
函数
(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.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 XML C++17 对std::*_ordering
类型的反向移植。
此头文件还包含在 C++17 中实现三向比较的函数。
Qt::compareThreeWay()
函数重载为内置 C++ 类型提供了三向比较功能。
qCompareThreeWay() 模板是通用的三向比较实现。它依赖Qt::compareThreeWay()
和 freecompareThreeWay()
函数来实现。
函数文档
[constexpr noexcept, since 6.7]
template <typename Enum, Qt::if_enum<Enum> = true> 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, Qt::if_floating_point<FloatType> = true, Qt::if_integral<IntType> = true> 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, Qt::if_integral<IntType> = true, Qt::if_floating_point<FloatType> = true> 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, Qt::if_floating_point<LeftFloat> = true, Qt::if_floating_point<RightFloat> = true> 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, Qt::if_integral<LeftInt> = true, Qt::if_integral<RightInt> = true> 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, Qt::if_compatible_pointers<T, U> = true> 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, Qt::if_compatible_pointers<T, U> = true> 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, Qt::if_compatible_pointers<T, U> = true> 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 中引入。
[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 XML 排序类型之一。此函数适用于 C++17 和 C++20。
实际返回的类型取决于LeftType
和RightType
。
注意: 只有在compareThreeWay()
实现了(LeftType, RightType)
对或反向(RightType, LeftType)
对时,此函数模板才可用。
此方法等同于
using Qt::compareThreeWay; return compareThreeWay(lhs, rhs);
其中Qt::compareThreeWay
是内置类型三向比较的 Qt XML 实现。
免费的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 。
© 2025 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.