QByteArrayView: make conversion to string_view constexpr

Both QByteArrayView and std::string_view are Literal Types, so the
conversion between them should be constexpr.

Amends 96d67da420697cee10bdc537a1a592f6f22e2b8f.

Found in API-review.

Change-Id: Ic513ce32aa2a743ca890dc05a683a62c0f3a7d50
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
(cherry picked from commit f9474364ee7ad3209873530b786bc6c081e1e2c9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-11-27 06:39:59 +01:00 committed by Qt Cherry-pick Bot
parent c3ec6c8a53
commit 9dc6be95f0
2 changed files with 35 additions and 1 deletions

View File

@ -302,7 +302,7 @@ public:
[[nodiscard]] constexpr char front() const { Q_ASSERT(!empty()); return m_data[0]; }
[[nodiscard]] constexpr char back() const { Q_ASSERT(!empty()); return m_data[m_size - 1]; }
[[nodiscard]] Q_IMPLICIT operator std::string_view() const noexcept
[[nodiscard]] constexpr Q_IMPLICIT operator std::string_view() const noexcept
{ return std::string_view(m_data, size_t(m_size)); }
//

View File

@ -197,6 +197,10 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.isEmpty());
static_assert(bv.data() == nullptr);
constexpr std::string_view sv = bv;
static_assert(sv.size() == 0);
static_assert(sv.data() == nullptr);
constexpr QByteArrayView bv2(bv.data(), bv.data() + bv.size());
static_assert(bv2.isNull());
static_assert(bv2.empty());
@ -209,6 +213,10 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.isEmpty());
static_assert(bv.data() != nullptr);
constexpr std::string_view sv = bv;
static_assert(sv.size() == bv.size());
static_assert(sv.data() == bv.data());
constexpr QByteArrayView bv2(bv.data(), bv.data() + bv.size());
static_assert(!bv2.isNull());
static_assert(bv2.empty());
@ -241,6 +249,14 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.rbegin() != bv.rend());
static_assert(bv.crbegin() != bv.crend());
constexpr std::string_view sv = bv;
static_assert(sv.size() == bv.size());
static_assert(sv.data() == bv.data());
#ifdef AMBIGUOUS_CALL // QTBUG-108805
static_assert(sv == bv);
static_assert(bv == sv);
#endif
constexpr QByteArrayView bv2(bv.data(), bv.data() + bv.size());
static_assert(!bv2.isNull());
static_assert(!bv2.empty());
@ -266,6 +282,13 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.at(4) == 'o');
static_assert(bv.back() == 'o');
static_assert(bv.last() == 'o');
constexpr std::string_view sv = bv;
static_assert(bv.size() == sv.size());
#ifdef AMBIGUOUS_CALL // QTBUG-108805
static_assert(bv == sv);
static_assert(sv == bv);
#endif
}
{
static constexpr char hello[] = { 'H', 'e', 'l', 'l', 'o' };
@ -283,6 +306,13 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.at(4) == 'o');
static_assert(bv.back() == 'o');
static_assert(bv.last() == 'o');
constexpr std::string_view sv = bv;
static_assert(bv.size() == sv.size());
#ifdef AMBIGUOUS_CALL // QTBUG-108805
static_assert(bv == sv);
static_assert(sv == bv);
#endif
}
#endif
{
@ -291,6 +321,10 @@ void tst_QByteArrayView::constExpr() const
static_assert(bv.isNull());
static_assert(bv.isEmpty());
static_assert(bv.size() == 0);
constexpr std::string_view sv = bv;
static_assert(sv.size() == 0);
static_assert(sv.data() == nullptr);
}
}