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>
bb10
Thiago Macieira 2024-03-04 12:49:43 -08:00
parent 025c45d628
commit 21dfb0ebcc
2 changed files with 4 additions and 2 deletions

View File

@ -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 {

View File

@ -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); }