diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2024-03-04 12:49:43 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2024-03-12 17:23:03 -0800 |
commit | 21dfb0ebcc6b3a3408a8272ce536a1e4d53910cd (patch) | |
tree | 54fc19df99e78b052f38fa14ae621402bea3dde4 | |
parent | tst_QStorageInfo: try harder to make the free space change (diff) | |
download | qtbase-21dfb0ebcc6b3a3408a8272ce536a1e4d53910cd.tar.xz qtbase-21dfb0ebcc6b3a3408a8272ce536a1e4d53910cd.zip |
QString/QByteArray: add explicit constructors for Q{String,ByteArray}View
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 <marten.nordheim@qt.io>
-rw-r--r-- | src/corelib/text/qbytearray.h | 3 | ||||
-rw-r--r-- | 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<qint64>(*this, ok, base); } |