diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2023-05-21 13:38:09 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2023-07-13 16:19:06 +0200 |
commit | 96d67da420697cee10bdc537a1a592f6f22e2b8f (patch) | |
tree | 124ac8c38b4e670ad397146ad6ae17cebc08789e /src/corelib/text/qstring.h | |
parent | c07a3f10ef27b42ab2f87bde74d86831c79317cf (diff) |
String-like containers: add implicit conversions towards std:: string views
In C++20 std::basic_string_view has gained a range constructor (like
QStringView always had), but that range constructor has been made
explicit. This means we can't just pass a QString(View) to a function
taking a u16string_view. The consensus seems to be that that types that
should implictly convert towards stdlib's string views should do that
via implicit conversion operators. This patch adds them for
* QByteArrayView => std::string_view
* QString(View) => std::u16string_view
* QUtf8StringView => std::string_view or std::u8string_view, depending
on the storage_type
QLatin1StringView doesn't have a matching std:: view so I'm not enabling
its conversion.
QByteArray poses a challenge, in that it already defines a conversion
towards const char *. (One can disable that conversion with a macro.)
That conversion makes it impossible to support:
QByteArray ba;
std::string_view sv1(ba); // 1
std::string_view sv2 = ba; // 2
because:
* if only operator const char *() is defined, then (2) doesn't work
(situation right now);
* if both conversions to const char * and string_view are defined, then
(1) is ambiguous on certain compilers (MSVC, QCC). Interestingly
enough, not on GCC/Clang, but only in C++17 and later modes.
I can't kill the conversion towards const char * (API break, and we use
it *everywhere* in Qt), hence, QByteArray does not get the implicit
conversion, at least not in this patch.
[ChangeLog][QtCore][QByteArrayView] Added an implicit conversion
operator towards std::string_view.
[ChangeLog][QtCore][QString] Added an implicit conversion operator
towards std::u16string_view.
[ChangeLog][QtCore][QStringView] Added an implicit conversion operator
towards std::u16string_view.
[ChangeLog][QtCore][QUtf8StringView] Added an implicit conversion
operator towards std::string_view (QUtf8StringView is using char
as its storage type in Qt 6). Note that QUtf8StringView is planned to
use char8_t in Qt 7, therefore it is expected that the conversion will
change towards std::u8string_view in Qt 7.
Change-Id: I6d3b64d211a386241ae157765cd1b03f531f909a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qstring.h')
-rw-r--r-- | src/corelib/text/qstring.h | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index e050ec356e..602bbb3e97 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -883,6 +883,8 @@ public: static inline QString fromStdU32String(const std::u32string &s); inline std::u32string toStdU32String() const; + Q_IMPLICIT inline operator std::u16string_view() const noexcept; + #if defined(Q_OS_DARWIN) || defined(Q_QDOC) static QString fromCFString(CFStringRef string); CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED; @@ -1351,6 +1353,11 @@ inline std::u32string QString::toStdU32String() const return u32str; } +QString::operator std::u16string_view() const noexcept +{ + return std::u16string_view(d.data(), size_t(d.size)); +} + #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED) Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QString &); Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &); |