summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-11-22 18:25:44 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2020-11-25 11:56:44 +0000
commit647194312fd26ca357105970bf16abd249086826 (patch)
treea7a139867b71b4f24039d797b3bdfa4cd10d4b09 /tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
parent5fb3b9e2606ad9bc3728ebd9c1d190a32b270d68 (diff)
Fix QVariant/QMetaType::compare APIs
std::optional<int> is the wrong datatype to use for compare. First and foremost, it can't be used in the idiomatic form of auto r = a.compare(b); if (r < 0) ~~~ // a is less than b if (r > 0) ~~~ // a is greater than b which we *already* feature in Qt (QString, QByteArray). Also, std::optional<int> (explicitly) converts to bool, which is a trap, because the result of the comparison can be accidentally tested as a bool: if (a.compare(b)) ~~~ // oops! does NOT mean a<b Not to mention extending this to algorithms: auto lessThan = [](QVariant a, QVariant b) { return a.compare(b); }; // oops! std::ranges::sort(vectorOfVariants, lessThan); which thankfully doesn't compile as is -- std::optional has an *explicit* operator bool, and the Compare concept requires an implicit conversion. However, the error the user is going to face will be "cannot convert to bool because the operator is explicit", which is deceiving because the fix is NOT supposed to be: auto lessThan = [](QVariant a, QVariant b) { return (bool)a.compare(b); }; // big oops! Instead: backport to Qt the required subset of C++20's <compare> API, and use that. This commits just adds the necessary parts for compare() (i.e. partial ordering), the rest of <compare> (classes, functions, conversions) can be added to 6.1. Change-Id: I2b5522da47854da39f79993e1207fad033786f00 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit 3e59c97c3453926fc66479d9ceca03901df55f90) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp')
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp6
1 files changed, 3 insertions, 3 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
index 58f1772e91..0273962636 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
@@ -2576,17 +2576,17 @@ void tst_QMetaType::compareCustomEqualOnlyType()
// check QMetaType::compare works/doesn't crash for equals only comparators
auto cmp = type.compare(variant50.constData(), variant50.constData());
- QVERIFY(!cmp);
+ QCOMPARE(cmp, QPartialOrdering::Unordered);
bool equals = type.equals(variant50.constData(), variant50.constData());
QVERIFY(equals);
cmp = type.compare(variant100.constData(), variant100x.constData());
- QVERIFY(!cmp);
+ QCOMPARE(cmp, QPartialOrdering::Unordered);
equals = type.equals(variant100.constData(), variant100x.constData());
QVERIFY(equals);
cmp = type.compare(variant50.constData(), variant100.constData());
- QVERIFY(!cmp);
+ QCOMPARE(cmp, QPartialOrdering::Unordered);
equals = type.equals(variant50.constData(), variant100.constData());
QVERIFY(!equals);