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>
This commit is contained in:
parent
5e66ea373a
commit
50f3ccd782
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user