summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qcomparehelpers.h
Commit message (Collapse)AuthorAgeFilesLines
* compare helpers: use is_OP etc instead of OP 0Marc Mutz7 days1-14/+14
| | | | | | | | | | | | Older GCCs are otherwise going crazy with -Wzero-as-nullptr-constant. The is_OP() functions are specifically protected against this warning. Amends fe12650e9d85ea0ed4a73f85cdbef0ddf3b67ae3. Pick-to: 6.7 Change-Id: I4895e42be382c8549c1902db6f237d29f78bbe7e Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
* Comparison helper macros: add an Attributes parameterIvan Solovev2024-02-201-62/+156
| | | | | | | | | | | | | Some of relational operators in Qt are marked with QT_ASCII_CAST_WARN (see e.g. String, QLatin1StringView). If we want to convert these operators to the new comparison helper macros, we need a way to preserve this attribute. My tests show that simply adding the attribute to the helper comparesEqual() and compareThreeWay() functions does not work, so we need to explicitly add it to each of the generated operators. Change-Id: I2940a70fe191326e8a2ebfb05b8da6e0f21a845c Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* Add missing <=> 0 operator to Qt ordering typesMarc Mutz2023-12-071-0/+5
| | | | | | | | | | | | | | | It's required by the standard, see e.g. [1] and the eel.is links in the code. [ChangeLog][QtCore][QPartialOrdering] Added three-way comparison operator (<=>) against literal zero, available when compiling in C++20 mode. [1] https://en.cppreference.com/w/cpp/utility/compare/partial_ordering#Comparisons Change-Id: I8a3b76661400930c6e247cf5b138ff52bf784395 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
* qcomparehelper.h: simplify compareThreeWay() SFINAE helpersMarc Mutz2023-12-071-20/+12
| | | | | | | | | | | | | There's no restriction on the number of enable_if's in a template-initializer, so simplify the compareThreeWay() constraints by having separate constraints each for LeftType and RightType. Fewer templates to instantiate = faster compile speed. As a drive-by, drop remove_reference_t. We control all users and all of them pass already-decayed types. Change-Id: I17c01c7aa1ac03bb6db4b0bef1371ebc0641641d Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
* Teach Qt::compareThreeWay() to support native float16 typesIvan Solovev2023-12-071-3/+11
| | | | | | | | | | Provide a custom variable template to detect float types and specialize it for QtPrivate::NativeFloat16Type. This will allow to enable three-way comparison for qfloat16. Task-number: QTBUG-104113 Change-Id: Id12c42c86f8dc9e233fe39776b0f0e28088de9e1 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* tst_QCompareHelpers: extract Qt/std-mapping into public headerMarc Mutz2023-12-051-0/+29
| | | | | | | We'll need this elsehere, too. Change-Id: I91a35a23dd201f7867898cee5b4d6743883f71fc Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
* Implement helper Qt::compareThreeWay() function for built-in typesIvan Solovev2023-11-281-0/+132
| | | | | | | | | | | | | | | | | | | | | | | | | 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>
* Implement compare helper macrosIvan Solovev2023-11-281-0/+307
These macros should unwrap into a proper set of equality and ordering operators, depending on the C++ standard being used. For C++17, all 6 operators (==, !=, <, >, <=, >=) are overloaded, while for C++20 only the overloads for opeartor==() and operator<=>() are provided. The macros are documented as internal for now. The macros rely on two helper functions: bool comparesEqual(LeftType lhs, RightType rhs); ReturnType compareThreeWay(LeftType lhs, RightType rhs); The comparesEqual() helper function is used to implement operator==() and operator!=(). The compareThreeWay() helper function is used to implement the four relational operators in C++17, or operator<=>() in C++20. ReturnType must be one of Qt::{partial,weak,strong}_ordering. When possible, the functions should also be declared constexpr and noexcept. It's the user's responsibility to provide the functions before using the macros. Implement a test case which applies the new macros to the dummy classes, and uses the new helper function to verify the comparison results. The MSVC compiler before version 19.36 has a bug, where it fails to correctly generate reverse opeerators in C++20 mode. Introduce a new Q_COMPILER_LACKS_THREE_WAY_COMPARE_SYMMETRY definition for such compiler versions, and use it to manually generate reversed operators when needed. Task-number: QTBUG-104113 Change-Id: Idc19d55df011fd616ff654f35a964e831b8ab93b Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io>