diff options
author | Ville Voutilainen <ville.voutilainen@qt.io> | 2021-04-27 11:30:47 +0300 |
---|---|---|
committer | Ville Voutilainen <ville.voutilainen@qt.io> | 2021-04-28 23:33:06 +0300 |
commit | 0e4cc15da330fb25b913fbc8089e60f80a08f49f (patch) | |
tree | cd961968ba314f1297748537d7dd7794f042cf36 | |
parent | 776734576c2ba7bd4bdba6e09bc5dad5e093670a (diff) |
Fix comparison between nullptr and QWeakPointer
The comparison between nullptr and QWeakPointer was just bogus
and ill-formed. The INTEGRITY compiler catches that even if
nothing tries to use the comparison. It is an ill-formed, no
diagnostic required case of a function template never being
able to produce a valid specialization. And while we're at
it, this patch makes the result of comparing a nullptr to
a QWeakPointer or vice versa the same as asking .isNull() from
the weak pointer, because it seems mind-boggling if those
are not the same operation.
Task-number: QTBUG-93093
Change-Id: I0cc80e795c9af2be1b76de05157aa458ef260f2e
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/tools/qsharedpointer_impl.h | 12 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp | 15 |
2 files changed, 24 insertions, 3 deletions
diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 222aecb018..cf88456096 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -457,6 +457,7 @@ public: DECLARE_COMPARE_SET(const QSharedPointer &p1, p1.data(), std::nullptr_t, nullptr) DECLARE_COMPARE_SET(std::nullptr_t, nullptr, const QSharedPointer &p2, p2.data()) #undef DECLARE_TEMPLATE_COMPARE_SET +#undef DECLARE_COMPARE_SET private: explicit QSharedPointer(Qt::Initialization) {} @@ -658,9 +659,14 @@ public: friend bool operator!=(const QSharedPointer<X> &p1, const QWeakPointer &p2) noexcept { return p2 != p1; } - DECLARE_COMPARE_SET(const QWeakPointer &p1, p1.d, std::nullptr_t, nullptr) - DECLARE_COMPARE_SET(std::nullptr_t, nullptr, const QWeakPointer &p2, p2.data()) -#undef DECLARE_COMPARE_SET + friend bool operator==(const QWeakPointer &p, std::nullptr_t) + { return p.isNull(); } + friend bool operator==(std::nullptr_t, const QWeakPointer &p) + { return p.isNull(); } + friend bool operator!=(const QWeakPointer &p, std::nullptr_t) + { return !p.isNull(); } + friend bool operator!=(std::nullptr_t, const QWeakPointer &p) + { return !p.isNull(); } private: friend struct QtPrivate::EnableInternalData; diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp index 42df800b14..db67ab3a5a 100644 --- a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp @@ -341,6 +341,11 @@ void tst_QSharedPointer::basics() QCOMPARE(!weak, isNull); QCOMPARE(bool(weak), !isNull); + QCOMPARE(weak.isNull(), (weak == nullptr)); + QCOMPARE(weak.isNull(), (nullptr == weak)); + QCOMPARE(!weak.isNull(), (weak != nullptr)); + QCOMPARE(!weak.isNull(), (nullptr != weak)); + QVERIFY(ptr == weak); QVERIFY(weak == ptr); QVERIFY(! (ptr != weak)); @@ -426,6 +431,12 @@ void tst_QSharedPointer::nullptrOps() QVERIFY(!p2.get()); QVERIFY(p1 == p2); + QWeakPointer<char> wp1 = p1; + QVERIFY(wp1 == nullptr); + QVERIFY(nullptr == wp1); + QCOMPARE(wp1, nullptr); + QCOMPARE(nullptr, wp1); + QSharedPointer<char> p3 = p1; QVERIFY(p3 == p1); QVERIFY(p3 == null); @@ -452,6 +463,10 @@ void tst_QSharedPointer::nullptrOps() QVERIFY(p4 != p2); QVERIFY(p4 != null); QVERIFY(p4 != p3); + + QWeakPointer<char> wp2 = p4; + QVERIFY(wp2 != nullptr); + QVERIFY(nullptr != wp2); } void tst_QSharedPointer::swap() |