From 21dfb0ebcc6b3a3408a8272ce536a1e4d53910cd Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 4 Mar 2024 12:49:43 -0800 Subject: [PATCH] QString/QByteArray: add explicit constructors for Q{String,ByteArray}View MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit std::string has them too, see constructor 10 in [1]. There, they are StringViewLike, something we may want to do here too, which would also lower the precedence of the constructor in the overload resolution. This will be needed for the non-const heterogeneous QHash::operator[]. [ChangeLog][QtCore][QByteArray] Added a constructor to create a QByteArray from QByteArrayView. [ChangeLog][QtCore][QString] Added a constructor to create a QString from QStringView. [1] https://en.cppreference.com/w/cpp/string/basic_string/basic_string Change-Id: I6818d78a57394e37857bfffd17b9ab03bd0253e6 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/text/qbytearray.h | 3 ++- src/corelib/text/qstring.h | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 710eb65c66..3c8a3bba45 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -92,6 +92,7 @@ public: QByteArray(const char *, qsizetype size = -1); QByteArray(qsizetype size, char c); QByteArray(qsizetype size, Qt::Initialization); + explicit QByteArray(QByteArrayView v) : QByteArray(v.data(), v.size()) {} inline QByteArray(const QByteArray &) noexcept; inline ~QByteArray(); @@ -794,7 +795,7 @@ qsizetype erase_if(QByteArray &ba, Predicate pred) // QByteArray QByteArrayView::toByteArray() const { - return QByteArray(data(), size()); + return QByteArray(*this); } namespace Qt { diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 198b63c995..854850a981 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -167,6 +167,7 @@ public: QString(QChar c); QString(qsizetype size, QChar c); inline QString(QLatin1StringView latin1); + explicit QString(QStringView sv) : QString(sv.data(), sv.size()) {} #if defined(__cpp_char8_t) || defined(Q_QDOC) Q_WEAK_OVERLOAD inline QString(const char8_t *str) @@ -1118,7 +1119,7 @@ int QStringView::compare(QUtf8StringView other, Qt::CaseSensitivity cs) const no // QString QStringView::toString() const -{ return QString(data(), size()); } +{ return QString(*this); } qint64 QStringView::toLongLong(bool *ok, int base) const { return QString::toIntegral_helper(*this, ok, base); }