summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-01-06 18:16:37 +0100
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-01-09 00:01:55 +0100
commit50f3ccd7829b2d98f30d362f017f76509058f0fc (patch)
treec6b6e687a768ff06ccd7f64a205f3f276881218d /tests
parent5e66ea373a581d7e62b829d0ffa7f84d754c7f8f (diff)
QStringView: add a test for overload resolution versus QString
In the presence of multiple overloads of a function taking either QString or QStringView, QStringView should always be preferred. The rationale is that the QStringView overload may have been added "later" (read: the function was written when QStringView was not available yet, so it took QString), and the fact that a function with the _same name_ offers a QStringView overload implies the function never needed to store/own the string in the first place. Add a (compile-time) test for this preference. This is in preparation for a future QString(char16_t*) constructor (in Qt 5.15 / Qt 6). Change-Id: I60a435e494b653548f8f8d52c5d7e7cac2cc875a Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/text/qstringview/tst_qstringview.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp
index 47ce9a6f63..631bcce508 100644
--- a/tests/auto/corelib/text/qstringview/tst_qstringview.cpp
+++ b/tests/auto/corelib/text/qstringview/tst_qstringview.cpp
@@ -221,6 +221,8 @@ private Q_SLOTS:
void comparison();
+ void overloadResolution();
+
private:
template <typename String>
void conversion_tests(String arg) const;
@@ -678,5 +680,61 @@ void tst_QStringView::comparison()
QVERIFY(bb.compare(aa) > 0);
}
+namespace QStringViewOverloadResolution {
+static void test(QString) = delete;
+static void test(QStringView) {}
+}
+
+// Compile-time only test: overload resolution prefers QStringView over QString
+void tst_QStringView::overloadResolution()
+{
+ {
+ QChar qcharArray[42] = {};
+ QStringViewOverloadResolution::test(qcharArray);
+ QChar *qcharPointer = qcharArray;
+ QStringViewOverloadResolution::test(qcharPointer);
+ }
+
+ {
+ ushort ushortArray[42] = {};
+ QStringViewOverloadResolution::test(ushortArray);
+ ushort *ushortPointer = ushortArray;
+ QStringViewOverloadResolution::test(ushortPointer);
+ }
+
+ {
+ QStringRef stringRef;
+ QStringViewOverloadResolution::test(stringRef);
+ QStringViewOverloadResolution::test(qAsConst(stringRef));
+ QStringViewOverloadResolution::test(std::move(stringRef));
+ }
+
+#if defined(Q_OS_WIN)
+ {
+ wchar_t wchartArray[42] = {};
+ QStringViewOverloadResolution::test(wchartArray);
+ QStringViewOverloadResolution::test(L"test");
+ }
+#endif
+
+#if defined(Q_COMPILER_UNICODE_STRINGS)
+ {
+ char16_t char16Array[] = u"test";
+ QStringViewOverloadResolution::test(char16Array);
+ char16_t *char16Pointer = char16Array;
+ QStringViewOverloadResolution::test(char16Pointer);
+ }
+#endif
+
+#if defined(Q_STDLIB_UNICODE_STRINGS)
+ {
+ std::u16string string;
+ QStringViewOverloadResolution::test(string);
+ QStringViewOverloadResolution::test(qAsConst(string));
+ QStringViewOverloadResolution::test(std::move(string));
+ }
+#endif
+}
+
QTEST_APPLESS_MAIN(tst_QStringView)
#include "tst_qstringview.moc"