diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index 695a6f8b39..f5ad53aa54 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -86,6 +86,20 @@ public: return *this; } +#ifdef Q_COMPILER_RVALUE_REFS + QArrayDataPointer(QArrayDataPointer &&other) + : d(other.d) + { + other.d = Data::sharedNull(); + } + + QArrayDataPointer &operator=(QArrayDataPointer &&other) + { + this->swap(other); + return *this; + } +#endif + DataOps &operator*() const { Q_ASSERT(d); diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 6a42f4da59..00873a84d1 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -84,6 +84,7 @@ private slots: void fromRawData(); void literals(); void variadicLiterals(); + void rValueReferences(); }; template const T &const_(const T &t) { return t; } @@ -1274,5 +1275,50 @@ void tst_QArrayData::variadicLiterals() #endif // defined(Q_COMPILER_VARIADIC_MACROS) } +#ifdef Q_COMPILER_RVALUE_REFS +// std::remove_reference is in C++11, but requires library support +template struct RemoveReference { typedef T Type; }; +template struct RemoveReference { typedef T Type; }; + +// single-argument std::move is in C++11, but requires library support +template +typename RemoveReference::Type &&cxx11Move(T &&t) +{ + return static_cast::Type &&>(t); +} +#endif + +void tst_QArrayData::rValueReferences() +{ +#ifdef Q_COMPILER_RVALUE_REFS + SimpleVector v1(1, 42); + SimpleVector v2; + + const SimpleVector::const_iterator begin = v1.constBegin(); + + QVERIFY(!v1.isNull()); + QVERIFY(v2.isNull()); + + // move-assign + v2 = cxx11Move(v1); + + QVERIFY(v1.isNull()); + QVERIFY(!v2.isNull()); + QCOMPARE(v2.constBegin(), begin); + + SimpleVector v3(cxx11Move(v2)); + + QVERIFY(v1.isNull()); + QVERIFY(v2.isNull()); + QVERIFY(!v3.isNull()); + QCOMPARE(v3.constBegin(), begin); + + QCOMPARE(v3.size(), size_t(1)); + QCOMPARE(v3.front(), 42); +#else + QSKIP("RValue references are not supported in current configuration"); +#endif +} + QTEST_APPLESS_MAIN(tst_QArrayData) #include "tst_qarraydata.moc"