From 50f3ccd7829b2d98f30d362f017f76509058f0fc Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 6 Jan 2020 18:16:37 +0100 Subject: 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 Reviewed-by: Thiago Macieira --- .../corelib/text/qstringview/tst_qstringview.cpp | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'tests/auto') 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 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" -- cgit v1.2.3