diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-05-15 11:02:50 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-05-22 11:22:40 +0200 |
commit | cf7d990a486b406d558e3291247d4323a9f48c73 (patch) | |
tree | e6a3e55184ff6664aad2e09b72f9ae31755b556f /tests | |
parent | 0e2c013a4523915a0af37fe1f338a4f66b07f91d (diff) |
Fix reference to a dead temporary
NoDefaultConstructorRef1 was taking a reference of the input, which
meant in the first test it would get a reference to the temporary
created by the 1 literal. A temporary that would be out of scope by
the time we check its value.
Instead add a test with unique_ptr to test we can pass movable
temporaries.
Change-Id: I6b02377dfe30c82b6e71bfb3353a81ad81558ed3
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp index ade9c5e754..e97848fb1c 100644 --- a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp @@ -40,6 +40,7 @@ #include "nontracked.h" #include "wrapper.h" +#include <memory> #include <stdlib.h> #include <time.h> @@ -232,6 +233,12 @@ struct NoDefaultConstructorRRef1 int &i; NoDefaultConstructorRRef1(int &&i) : i(i) {} }; + +struct NoDefaultConstructorRRef2 +{ + std::unique_ptr<int> i; + NoDefaultConstructorRRef2(std::unique_ptr<int> &&i) : i(std::move(i)) {} +}; #endif void tst_QSharedPointer::basics_data() @@ -1820,15 +1827,20 @@ void tst_QSharedPointer::creatingVariadic() QCOMPARE(&ptr->i, &i); } { - NoDefaultConstructorRRef1(1); // control check - QSharedPointer<NoDefaultConstructorRRef1> ptr = QSharedPointer<NoDefaultConstructorRRef1>::create(1); - QCOMPARE(ptr->i, 1); - NoDefaultConstructorRRef1(std::move(i)); // control check - ptr = QSharedPointer<NoDefaultConstructorRRef1>::create(std::move(i)); + QSharedPointer<NoDefaultConstructorRRef1> ptr = QSharedPointer<NoDefaultConstructorRRef1>::create(std::move(i)); QCOMPARE(ptr->i, i); } { + NoDefaultConstructorRRef2(std::unique_ptr<int>(new int(1))); // control check + QSharedPointer<NoDefaultConstructorRRef2> ptr = QSharedPointer<NoDefaultConstructorRRef2>::create(std::unique_ptr<int>(new int(1))); + QCOMPARE(*ptr->i, 1); + + std::unique_ptr<int> p(new int(i)); + ptr = QSharedPointer<NoDefaultConstructorRRef2>::create(std::move(p)); + QCOMPARE(*ptr->i, i); + } + { QString text("Hello, World"); NoDefaultConstructorRef2(text, 1); // control check QSharedPointer<NoDefaultConstructorRef2> ptr = QSharedPointer<NoDefaultConstructorRef2>::create(text, 1); |