From cf7d990a486b406d558e3291247d4323a9f48c73 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Wed, 15 May 2019 11:02:50 +0200 Subject: 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 --- .../tools/qsharedpointer/tst_qsharedpointer.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'tests') 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 #include #include @@ -232,6 +233,12 @@ struct NoDefaultConstructorRRef1 int &i; NoDefaultConstructorRRef1(int &&i) : i(i) {} }; + +struct NoDefaultConstructorRRef2 +{ + std::unique_ptr i; + NoDefaultConstructorRRef2(std::unique_ptr &&i) : i(std::move(i)) {} +}; #endif void tst_QSharedPointer::basics_data() @@ -1820,14 +1827,19 @@ void tst_QSharedPointer::creatingVariadic() QCOMPARE(&ptr->i, &i); } { - NoDefaultConstructorRRef1(1); // control check - QSharedPointer ptr = QSharedPointer::create(1); - QCOMPARE(ptr->i, 1); - NoDefaultConstructorRRef1(std::move(i)); // control check - ptr = QSharedPointer::create(std::move(i)); + QSharedPointer ptr = QSharedPointer::create(std::move(i)); QCOMPARE(ptr->i, i); } + { + NoDefaultConstructorRRef2(std::unique_ptr(new int(1))); // control check + QSharedPointer ptr = QSharedPointer::create(std::unique_ptr(new int(1))); + QCOMPARE(*ptr->i, 1); + + std::unique_ptr p(new int(i)); + ptr = QSharedPointer::create(std::move(p)); + QCOMPARE(*ptr->i, i); + } { QString text("Hello, World"); NoDefaultConstructorRef2(text, 1); // control check -- cgit v1.2.3