qt5base-lts/tests/auto/corelib
Giuseppe D'Angelo 405244fe30 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>
2020-11-30 17:16:22 +01:00
..
animation Replace discouraged Q_MOVABLE_TYPE by Q_RELOCATABLE_TYPE 2020-11-30 17:16:21 +01:00
global Fix QVariant/QMetaType::compare APIs 2020-11-30 17:16:22 +01:00
io QStandardPaths: Don't change permissions of XDG_RUNTIME_DIR 2020-11-30 19:16:21 +03:00
itemmodels Fix invalid QSortFilterProxyModel::dataChanged parameters 2020-11-30 12:39:21 +08:00
kernel Fix QVariant/QMetaType::compare APIs 2020-11-30 17:16:22 +01:00
mimetypes CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
plugin Fix tst_qlibrary after rebuild 2020-11-24 21:50:46 +01:00
serialization Test conversion of ulonglong variant to JSON 2020-11-20 14:31:49 +01:00
text QCollator: extend tests 2020-11-30 17:16:21 +01:00
thread Add convenience functions for QFuture creation 2020-11-23 09:50:21 +01:00
time QCalendar: increase coverage by tests 2020-11-27 13:26:52 +01:00
tools QHash: support std::hash as hashing function 2020-11-30 17:16:22 +01:00
.prev_CMakeLists.txt Move QStateMachine from QtCore to QtScxml 2020-08-24 20:10:25 +02:00
CMakeLists.txt Move QStateMachine from QtCore to QtScxml 2020-08-24 20:10:25 +02:00
corelib.pro Move QStateMachine from QtCore to QtScxml 2020-08-24 20:10:25 +02:00