Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-118115

As a user of QAnyStringView, I'd like to use it in constexpr contexts

    XMLWordPrintable

Details

    • User Story
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • None
    • None

    Description

      QAnyStringView is supported to be a literal type, but the following fails to build:

      #include <QtCore>
      
      struct Tag
      {
          static constexpr Tag fromString(QAnyStringView view) {
              return Tag{view.visit([](auto view){
                  return view.size();
              })};
          }
      
          constexpr qsizetype size() const { return m_size; }
      
          qsizetype m_size;
      };
      
      int main()
      {
          constexpr qsizetype size = Tag::fromString(u"abcd").size();
          return 0;
      }
      

      with error

      main.cpp:18:25: error: constexpr variable 'size' must be initialized by a constant expression
          constexpr qsizetype size = Tag::fromString(u"abcd").size();
                              ^      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      /Users/vohi/qt/dev-build/qtbase/include/QtCore/../../../../dev/qtbase/src/corelib/text/qanystringview.h:324:47: note: read of member 'm_data_utf16' of union with active member 'm_data' is not allowed in a constant expression
          { return Q_ASSERT(isUtf16()), QStringView{m_data_utf16, size()}; }
                                                    ^
      /Users/vohi/qt/dev-build/qtbase/include/QtCore/../../../../dev/qtbase/src/corelib/text/qstring.h:105:41: note: in call to '&view->asStringView()'
              return std::forward<Visitor>(v)(asStringView());
                                              ^
      /Users/vohi/qt/testing/sandbox/main.cpp:6:25: note: in call to '&view->visit([](auto view) {
          return view.size();
      })'
              return Tag{view.visit([](auto view){
                              ^
      /Users/vohi/qt/testing/sandbox/main.cpp:18:32: note: in call to 'fromString({{.m_data = &u"abcd"[0]}, 9223372036854775812})'
          constexpr qsizetype size = Tag::fromString(u"abcd").size();
                                     ^
      1 error generated.
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            mmutz Marc Mutz
            vhilshei Volker Hilsheimer
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes