diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2024-03-07 16:24:00 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2024-03-12 21:51:43 +0100 |
commit | 285a2a75b4342e73d3165378d7af6c48d2f06492 (patch) | |
tree | d700a011c7ad6114300f57589488f02a1bf88987 /src/corelib/kernel | |
parent | 33159f5e0f18a6d9f4ca015bfb9a2a53089d1dfa (diff) |
QVariant: use comparison helper macros
The relational operators were removed in
8652c79df0a47264a2d525424484e15744e2462b with the argument that they
do not have a total order. Back than it was a valid argument, because Qt
did not support any of the C++20 ordering types.
Now Qt has its own implementation for all three ordering types, so we
could technically add relational operators and claim that QVariant
provides partial ordering.
However, that could potentially lead to many bugs and/or unexpected
results, if people start using QVariant as a key in std::map/QMap
containers.
We do not want that to happen, so we only use the helper macros to
implement (in)equality operators.
This commit also extends the unit tests, providing more extensive
testing of (in)equality operators and the pre-existing
QVariant::compare() method.
Fixes: QTBUG-113234
Change-Id: I783f3b5df552da782627f4ed0a5bb1b577753a23
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 9 | ||||
-rw-r--r-- | src/corelib/kernel/qvariant.h | 7 |
2 files changed, 9 insertions, 7 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 270d2642e9..92a44c462b 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -324,6 +324,7 @@ static QVariant::Private clonePrivate(const QVariant::Private &other) \ingroup objectmodel \ingroup shared + \compares equality Because C++ forbids unions from including types that have non-default constructors or destructors, most interesting Qt @@ -2152,9 +2153,9 @@ bool QVariant::view(int type, void *ptr) } /*! - \fn bool QVariant::operator==(const QVariant &v1, const QVariant &v2) + \fn bool QVariant::operator==(const QVariant &lhs, const QVariant &rhs) - Returns \c true if \a v1 and \a v2 are equal; otherwise returns \c false. + Returns \c true if \a lhs and \a rhs are equal; otherwise returns \c false. QVariant uses the equality operator of the type() contained to check for equality. @@ -2178,9 +2179,9 @@ bool QVariant::view(int type, void *ptr) */ /*! - \fn bool QVariant::operator!=(const QVariant &v1, const QVariant &v2) + \fn bool QVariant::operator!=(const QVariant &lhs, const QVariant &rhs) - Returns \c false if \a v1 and \a v2 are equal; otherwise returns \c true. + Returns \c false if \a lhs and \a rhs are equal; otherwise returns \c true. QVariant uses the equality operator of the type() contained to check for equality. diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 6d51d940c5..9f0edf4233 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -5,6 +5,7 @@ #define QVARIANT_H #include <QtCore/qatomic.h> +#include <QtCore/qcompare.h> #include <QtCore/qcontainerfwd.h> #include <QtCore/qmetatype.h> #ifndef QT_NO_DEBUG_STREAM @@ -614,10 +615,10 @@ private: return std::visit(visitor, std::forward<StdVariant>(v)); } - friend inline bool operator==(const QVariant &a, const QVariant &b) + friend bool comparesEqual(const QVariant &a, const QVariant &b) { return a.equals(b); } - friend inline bool operator!=(const QVariant &a, const QVariant &b) - { return !a.equals(b); } + Q_DECLARE_EQUALITY_COMPARABLE(QVariant) + #ifndef QT_NO_DEBUG_STREAM template <typename T> friend auto operator<<(const QDebug &debug, const T &variant) -> std::enable_if_t<std::is_same_v<T, QVariant>, QDebug> { |