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>
This commit is contained in:
Ville Voutilainen 2021-04-27 11:30:47 +03:00
parent 776734576c
commit 0e4cc15da3
2 changed files with 24 additions and 3 deletions

View File

@ -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;

View File

@ -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()