From 6c504f2519e1180dbcfd77d5bb08b0db9742eeaa Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sat, 23 Sep 2023 18:06:15 +0200 Subject: QPointer: also provide a converting assignment operator d026fad3d962eed0119351cd37f34490e09153fd added converting constructors for QPointer. This however made converting _assignments_ ambiguous, introducing a regression for users coming from Qt < 6.6. This code: QPointer base; QPointer derived; base = derived; used to convert `derived` to `Derived *` (using the implicit conversion operator from `QPointer` to `Derived *`), and then the assignment operator for `QPointer` that took a `Base *`. The introduction of the conversion constructor in 6.6 makes it possible to convert `QPointer` to `QPointer`, and then fall back to the compiler-generated assignment operator for `QPointer`. The result is that the code above is now ambiguous and stops compiling. Fix this by adding a converting assignment operator for QPointer. I'm only adding the const-lvalue overload because the implementation requires going through the private QWeakPointer::assign helper. We cannot copy-assign or move-assign the inner QWeakPointer, as those assignments require lock()ing the QWeakPointer and that's not possible on a QObject-tracking QWeakPointer (but cf. QTBUG-117483). Assigning from a rvalue QPointer would mean calling assign() on the internal QWeakPointer _and_ clear the incoming QPointer, and that's strictly worse than the lvalue overload (where we just call assign()). Change-Id: I33fb2a22b3d5110284d78e3d7c6cc79a5b73b67b Pick-to: 6.6 6.6.0 Reviewed-by: Thiago Macieira --- .../auto/corelib/kernel/qpointer/tst_qpointer.cpp | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tests/auto/corelib/kernel') diff --git a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp index 332cf6ab71..7f01c6f964 100644 --- a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp +++ b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp @@ -57,6 +57,19 @@ void tst_QPointer::conversion() QCOMPARE_EQ(pio.get(), &file); QCOMPARE_EQ(pio, pf); QCOMPARE_EQ(pio.get(), pf.get()); + + // reset + pio = nullptr; + QCOMPARE_EQ(pio, nullptr); + QCOMPARE_EQ(pio.get(), nullptr); + + // copy-assignment + QCOMPARE_EQ(pf, &file); + pio = pf; + QCOMPARE_EQ(pio, &file); + QCOMPARE_EQ(pio.get(), &file); + QCOMPARE_EQ(pio, pf); + QCOMPARE_EQ(pio.get(), pf.get()); } // move-conversion: { @@ -67,6 +80,16 @@ void tst_QPointer::conversion() QCOMPARE_EQ(pf, nullptr); QCOMPARE_EQ(pio, &file); QCOMPARE_EQ(pio.get(), &file); + + // reset + pio = nullptr; + QCOMPARE_EQ(pio, nullptr); + QCOMPARE_EQ(pio.get(), nullptr); + + // move-assignment + pio = QPointer(&file); + QCOMPARE_EQ(pio, &file); + QCOMPARE_EQ(pio.get(), &file); } } -- cgit v1.2.3