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

Impact of C++20 std::span on our code

    XMLWordPrintable

Details

    • C++20 std::span @ Qt

    Description

      C++20 added a (non-string) general view type called std::span. Unlike string views, std::span can reference const as well as mutable data (depending on whether its first template argument is const or non-const), and can either be dynamically-sized or fixed-size (depending on its second, defaulted, argument, a size).

      This type affects Qt API in profound ways, because it enables non-string APIs to use NOI (Non-Owning Interface; TL;DW: replacing the use of owning containers in APIs through non-owning ones (= views)), and Qt APIs are traditionally packed with owning container types (QList, QString, QByteArray).

      This could be an exit strategy from having to maintain our own containers, something that, despite sizeable resources thrown at the problem, we've been very bad at in the past.

      This type would preferably cross the ABI boundary, so, pending the resolution of QTBUG-100276, we'd need to wait until we depend on C++20 in order to use spans. But there's always the option to split the span into (ptr, size) and use two variables when crossing the ABI boundary, in both ways:

      std::span<const QRect> QRegion::rects() const noexcept { return {begin(), end()}; }
      void QRegion::setRects(std::span<const QRect> rects) {
          setRects(rects.data(), rects.size());
      }
      

      At a minimum, we should refrain from introducing new API that monopolizes names that could be used for span-returning functions. QRegion::rects, e.g., was removed in Qt 6, so would be available for use now. QGradient::stops(), however, is a problem.

      So the very first order of business would be to come up with a standard of how to deal with this issue in existing APIs. E.g., should QGradient::stops(). The current habit of making the std-type-returning functions have ugly names no-one wants to use (remainingAsDuration(), filesystemAbsoluteFilePath()) is not going to scale. We positively want users to use the std::span APIs as soon as they can, so having aboninations a la QGradient::stopsAsSpan() or QGradient::spanStops() won't help.

      Attachments

        Issue Links

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

          Activity

            People

              cnn Qt Core & Network
              mmutz Marc Mutz
              Vladimir Minenko Vladimir Minenko
              Alex Blasche Alex Blasche
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes