diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h index 4a5e80fa18e..e18fb854699 100644 --- a/src/corelib/text/qanystringview.h +++ b/src/corelib/text/qanystringview.h @@ -61,10 +61,13 @@ private: }; template - using if_compatible_char = std::enable_if_t, QtPrivate::IsCompatibleChar8Type - >, bool>; + >; + + template + using if_compatible_char = std::enable_if_t::value, bool>; template using if_compatible_pointer = std::enable_if_t, QAnyStringView::Tag>, std::is_same, QAnyStringView>, // don't make a copy/move ctor std::is_pointer>, // const char*, etc + is_compatible_char, // don't create a QString/QByteArray, we have a ctor std::is_same, QByteArray>, std::is_same, QString> >>, @@ -143,6 +147,11 @@ private: static QChar toQChar(QChar ch) noexcept { return ch; } static QChar toQChar(QLatin1Char ch) noexcept { return ch; } + struct QCharContainer { // private, so users can't pass their own + explicit QCharContainer() = default; + QChar ch; + }; + explicit constexpr QAnyStringView(const void *d, qsizetype n, std::size_t sizeAndType) noexcept : m_data{d}, m_size{std::size_t(n) | (sizeAndType & TypeMask)} {} public: @@ -192,16 +201,16 @@ public: constexpr QAnyStringView(Container &&c, QtPrivate::wrapped_t &&capacity = {}) //noexcept(std::is_nothrow_constructible_v) : QAnyStringView(capacity = std::forward(c)) {} - template = true> constexpr QAnyStringView(const Char &c) noexcept : QAnyStringView{&c, 1} {} - constexpr QAnyStringView(const QChar &c) noexcept - : QAnyStringView{&c, 1} {} + template = true> + constexpr QAnyStringView(Char ch, QCharContainer &&capacity = QCharContainer()) noexcept + : QAnyStringView{&(capacity.ch = ch), 1} {} template , bool> = true> - constexpr QAnyStringView(Char c, Container &&capacity = {}) + constexpr QAnyStringView(Char c, Container &&capacity = {}) noexcept : QAnyStringView(capacity = QChar::fromUcs4(c)) {} constexpr QAnyStringView(QStringView v) noexcept diff --git a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp index a6a1f0499ea..0f3567eb4b9 100644 --- a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp +++ b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp @@ -702,13 +702,17 @@ void tst_QAnyStringView::fromCharacter(Char arg, qsizetype expectedSize) const // Need to re-create a new QASV(arg) each time, QASV(Char).data() dangles // after the end of the Full Expression: + static_assert(noexcept(QAnyStringView(arg)), + "If this fails, we may be creating a temporary QString/QByteArray"); + QCOMPARE(QAnyStringView(arg).size(), expectedSize); // QCOMPARE(QAnyStringView(arg), arg); // not all pairs compile, so do it manually: + // Check implicit conversion: const QChar chars[] = { - QAnyStringView(arg).front(), - QAnyStringView(arg).back(), + [](QAnyStringView v) { return v.front(); }(arg), + [](QAnyStringView v) { return v.back(); }(arg), }; switch (expectedSize) {