<QtCompare> - Classes and helpers for defining comparison operators

The <QtCompare> header file defines Qt::*_ordering types and helper macros for defining comparison operators. More...

Header: #include <QtCompare>

Functions

(since 6.7) auto qCompareThreeWay(const LeftType &lhs, const RightType &rhs)

Detailed Description

This header introduces the Qt::partial_ordering, Qt::weak_ordering, and Qt::strong_ordering types, which are Qt's C++17 backports of std::*_ordering types.

This header also contains functions for implementing three-way comparison in C++17.

The Qt::compareThreeWay() function overloads provide three-way comparison for built-in C++ types.

The qCompareThreeWay() template serves as a generic three-way comparison implementation. It relies on Qt::compareThreeWay() and free compareThreeWay() functions in its implementation.

Function Documentation

[since 6.7] template <typename LeftType, typename RightType> auto qCompareThreeWay(const LeftType &lhs, const RightType &rhs)

Performs the three-way comparison on lhs and rhs and returns one of the Qt ordering types as a result. This function is available for both C++17 and C++20.

The actual returned type depends on LeftType and RightType.

Note: This function template is only available when compareThreeWay() is implemented for the (LeftType, RightType) pair or the reversed (RightType, LeftType) pair.

This method is equivalent to

using Qt::compareThreeWay;
return compareThreeWay(lhs, rhs);

where Qt::compareThreeWay is the Qt implementation of three-way comparison for built-in types.

The free compareThreeWay functions should provide three-way comparison for custom types. The functions should return one of the Qt ordering types.

Qt provides compareThreeWay implementation for some of its types.

Note: Do not re-implement compareThreeWay() for Qt types, as more Qt types will get support for it in future Qt releases.

Use this function primarly in generic code, when you know nothing about LeftType and RightType.

If you know the types, use

  • Qt::compareThreeWay for built-in types
  • compareThreeWay for custom types

Use operator<=>() directly in code that will only be compiled with C++20 or later.

This function was introduced in Qt 6.7.

See also Qt::partial_ordering, Qt::weak_ordering, and Qt::strong_ordering.

© 2024 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.