summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2019-09-05 09:58:30 +0200
committerMilian Wolff <milian.wolff@kdab.com>2019-10-11 16:53:23 +0200
commit014d7ac65417ed9b5ffb85cca24d16564ff5005a (patch)
tree3eb24e42350bdbbf731d9300672a7ccdf50b2378 /tests/auto/corelib
parent472f5331ca8091b191944650d043a288dac7c3e8 (diff)
Q{Shared,Weak}Pointer: Reduce overload sets in implicit conversions
Only allow implicit conversions when the types involved are compatible. That means, only allow construction and copy assignment when the type X* is convertible to type T*. This is done using SFINAE and the std::is_convertible type trait, which makes the previous QSHAREDPOINTER_VERIFY_AUTO_CAST obsolete. This patch fixes compilation when a function is overloaded with Q{Shared,Weak}Pointer of different, incompatible types. Previously, this resulted in a compilation error due to an ambiguous overload. Change-Id: I069d22f3582e69842f14284d4f27827326597ca2 Fixes: QTBUG-75222 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r--tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp
index e97848fb1c..0fe24ed5cb 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 <array>
#include <memory>
#include <stdlib.h>
#include <time.h>
@@ -105,12 +106,15 @@ private slots:
void sharedFromThis();
void constructorThrow();
+ void overloads();
void threadStressTest_data();
void threadStressTest();
void validConstructs();
void invalidConstructs_data();
void invalidConstructs();
+
+
// let invalidConstructs be the last test, because it's the slowest;
// add new tests above this block
public slots:
@@ -2250,6 +2254,11 @@ void tst_QSharedPointer::invalidConstructs_data()
<< &QTest::QExternalTest::tryCompileFail
<< "QSharedPointer<Data> ptr(new Data, [](int *) {});\n";
#endif
+
+ QTest::newRow("incompatible-overload")
+ << &QTest::QExternalTest::tryCompileFail
+ << "void foo(QSharedPointer<DerivedData>) {}\n"
+ "void bar() { foo(QSharedPointer<Data>()); }\n";
}
void tst_QSharedPointer::invalidConstructs()
@@ -2748,5 +2757,50 @@ void tst_QSharedPointer::reentrancyWhileDestructing()
ReentrancyWhileDestructing::A obj;
}
+namespace {
+struct Base1 {};
+struct Base2 {};
+
+struct Child1 : Base1 {};
+struct Child2 : Base2 {};
+
+template<template<typename> class SmartPtr>
+struct Overloaded
+{
+ std::array<int, 1> call(const SmartPtr<const Base1> &)
+ {
+ return {};
+ }
+ std::array<int, 2> call(const SmartPtr<const Base2> &)
+ {
+ return {};
+ }
+ static const Q_CONSTEXPR uint base1Called = sizeof(std::array<int, 1>);
+ static const Q_CONSTEXPR uint base2Called = sizeof(std::array<int, 2>);
+
+ void test()
+ {
+#define QVERIFY_CALLS(expr, base) Q_STATIC_ASSERT(sizeof(call(expr)) == base##Called)
+ QVERIFY_CALLS(SmartPtr<Base1>{}, base1);
+ QVERIFY_CALLS(SmartPtr<Base2>{}, base2);
+ QVERIFY_CALLS(SmartPtr<const Base1>{}, base1);
+ QVERIFY_CALLS(SmartPtr<const Base2>{}, base2);
+ QVERIFY_CALLS(SmartPtr<Child1>{}, base1);
+ QVERIFY_CALLS(SmartPtr<Child2>{}, base2);
+ QVERIFY_CALLS(SmartPtr<const Child1>{}, base1);
+ QVERIFY_CALLS(SmartPtr<const Child2>{}, base2);
+#undef QVERIFY_CALLS
+ }
+};
+}
+
+void tst_QSharedPointer::overloads()
+{
+ Overloaded<QSharedPointer> sharedOverloaded;
+ sharedOverloaded.test();
+ Overloaded<QWeakPointer> weakOverloaded;
+ weakOverloaded.test();
+}
+
QTEST_MAIN(tst_QSharedPointer)
#include "tst_qsharedpointer.moc"