diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 30c78c2c4dc..b9ff33aca62 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -107,8 +107,10 @@ public: using rvalue_ref = T &&; #endif - DataPointer &data_ptr() { return d; } - const DataPointer &data_ptr() const { return d; } + DataPointer &data_ptr() & { return d; } + const DataPointer &data_ptr() const & { return d; } + DataPointer &&data_ptr() && { return std::move(d); } + // No current use-case for a `const &&` overload class const_iterator; class iterator { diff --git a/src/corelib/tools/qlist.qdoc b/src/corelib/tools/qlist.qdoc index ce7dabe39b2..ac0dd420157 100644 --- a/src/corelib/tools/qlist.qdoc +++ b/src/corelib/tools/qlist.qdoc @@ -1640,8 +1640,9 @@ */ /*! - \fn template QList::DataPointer &QList::data_ptr(); - \fn template const QList::DataPointer &QList::data_ptr() const; + \fn template QList::DataPointer &QList::data_ptr() &; + \fn template const QList::DataPointer &QList::data_ptr() const &; + \fn template QList::DataPointer QList::data_ptr() &&; \internal \since 6.10 diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index 9ebfd7aee05..cdd567f979d 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -265,6 +265,7 @@ private slots: void countCustom() const { count(); } void cpp17ctad() const; void data() const; + void reinterpreted() const; void emptyInt() const { empty(); } void emptyMovable() const { empty(); } void emptyCustom() const { empty(); } @@ -776,7 +777,7 @@ void tst_QList::assignEmpty() const using T = int; QList list; QList ref1 = list; - QVERIFY(list.d.needsDetach()); + QVERIFY(list.data_ptr().needsDetach()); list.assign(list.begin(), list.begin()); #if !defined Q_OS_QNX // QNX has problems with the empty istream_iterator @@ -784,7 +785,7 @@ void tst_QList::assignEmpty() const list.squeeze(); QCOMPARE_EQ(list.capacity(), 0); ref1 = list; - QVERIFY(list.d.needsDetach()); + QVERIFY(list.data_ptr().needsDetach()); list.assign(empty, empty); #endif } @@ -1340,6 +1341,29 @@ void tst_QList::data() const QVERIFY(!constVec.isDetached()); // const data() does not detach() } +void tst_QList::reinterpreted() const +{ + const QList expected = {char16_t(42), char16_t(43), char16_t(44)}; + { + QList t = {42, 43, 44}; + const auto size = t.size(); + QList x = std::move(t.data_ptr()).reinterpreted(); + + QVERIFY(t.data_ptr().isNull()); + QCOMPARE(x.size(), size); + QCOMPARE_EQ(x, expected); + } + { + QList t = {42, 43, 44}; + const auto size = t.size(); + QList x = std::move(t).data_ptr().reinterpreted(); + + QVERIFY(t.data_ptr().isNull()); + QCOMPARE(x.size(), size); + QCOMPARE_EQ(x, expected); + } +} + template void tst_QList::empty() const {