diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-03-27 11:22:44 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-03-29 12:29:35 +0000 |
commit | 718023606370d7c0f972acb0ba83df3383b8800a (patch) | |
tree | 80f7929207bc73a1c5732d382c55cca2f1986621 /tests/auto/corelib/tools/qstringview/tst_qstringview.cpp | |
parent | e9c06b25af47e869028c7a56a679fda7b4475f38 (diff) |
QStringView: improve manual overload management
We want to prevent
QStringView(QChar|QLatin1String|QByteArray|const char*)
from compiling as QStringView(QString(...)), so I added = delete'ed
ctors for these types to QStringView. However, that makes QStringView
participate in overload resolution for these types. Even if the
QStringView ctor will always fail to compile, the presence of these
ctors alone makes calls to functions overloaded on QString and
QStringView ambiguous:
f(QStringView);
f(QString);
f(foo); // ambiguous
f(QChar('f')) // ambiguous
f(QLatin1String(foo)); // ambiguous
f(QByteArray(foo)); // ambiguous
Fix by making the QString and QStringRef constructors templates
constrained to accept only these two types. This should also help to
move the QStringView definition to before the QString one (as soon as
we get rid of or start to ignore QString::Null), simplifying a lot of
code in qstring.h down the line.
This should also fix MSVC's accepting of two user-defined conversions
which caused static non-compile-tests to fail in the initial
QStringView patch, and which were therefore removed. This patch brings
them back.
Change-Id: I95ac38c0d31cd8c726f7e952017569d32e484413
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/auto/corelib/tools/qstringview/tst_qstringview.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qstringview/tst_qstringview.cpp | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qstringview/tst_qstringview.cpp b/tests/auto/corelib/tools/qstringview/tst_qstringview.cpp index 2e671040dc..3154e5ec9d 100644 --- a/tests/auto/corelib/tools/qstringview/tst_qstringview.cpp +++ b/tests/auto/corelib/tools/qstringview/tst_qstringview.cpp @@ -43,10 +43,21 @@ template <typename T> using CanConvert = std::is_convertible<T, QStringView>; +Q_STATIC_ASSERT(!CanConvert<QLatin1String>::value); +Q_STATIC_ASSERT(!CanConvert<const char*>::value); +Q_STATIC_ASSERT(!CanConvert<QByteArray>::value); + +// QStringView qchar_does_not_compile() { return QStringView(QChar('a')); } +// QStringView qlatin1string_does_not_compile() { return QStringView(QLatin1String("a")); } +// QStringView const_char_star_does_not_compile() { return QStringView("a"); } +// QStringView qbytearray_does_not_compile() { return QStringView(QByteArray("a")); } + // // QChar // +Q_STATIC_ASSERT(!CanConvert<QChar>::value); + Q_STATIC_ASSERT(CanConvert< QString >::value); Q_STATIC_ASSERT(CanConvert<const QString >::value); Q_STATIC_ASSERT(CanConvert< QString&>::value); @@ -62,6 +73,8 @@ Q_STATIC_ASSERT(CanConvert<const QStringRef&>::value); // ushort // +Q_STATIC_ASSERT(!CanConvert<ushort>::value); + Q_STATIC_ASSERT(CanConvert< ushort*>::value); Q_STATIC_ASSERT(CanConvert<const ushort*>::value); @@ -72,6 +85,8 @@ Q_STATIC_ASSERT(CanConvert<const ushort*>::value); #if !defined(Q_OS_WIN) || defined(Q_COMPILER_UNICODE_STRINGS) +Q_STATIC_ASSERT(!CanConvert<char16_t>::value); + Q_STATIC_ASSERT(CanConvert< char16_t*>::value); Q_STATIC_ASSERT(CanConvert<const char16_t*>::value); @@ -99,6 +114,8 @@ Q_CONSTEXPR bool CanConvertFromWCharT = #endif ; +Q_STATIC_ASSERT(!CanConvert<wchar_t>::value); + Q_STATIC_ASSERT(CanConvert< wchar_t*>::value == CanConvertFromWCharT); Q_STATIC_ASSERT(CanConvert<const wchar_t*>::value == CanConvertFromWCharT); |