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

Impact of C++20 char8_t on our code



    • C++20 char8_t @ Qt


      C++20 added the new char8_t} character type and changed the type of {{u8 character and string literals:

      • In C++17, u8' ' is a char, in C++20, it is a char8_t.
      • In C++17, u8"ä" is a const char[3], in C++20, it is a const char8_t[3].

      For Qt, this is both a heaven-sent and hell. It's a heaven-sent, because it gives us a static way to distinguish UTF-8 data. Together with C++17's std::byte, we could now reasonably distinguish binary data (std::byte) from UTF-8 strings (char8_t) and retire char and uchar to legacy status. We don't need the equivalent of QT_NO_CAST_FROM_ASCII for char8_t*, because the conversion is well-defined. It enables QUtf8String (QTBUG-98430). All good stuff.

      But it's also hell, because it forces us to re-think our APIs:

      1. const char* function arguments break when called with u8"" string literals in C++20
        • needs overloading with a (Q_WEAK_OVERLOAD) const char8_t* version
        • or porting to QByteArray(View), if the data is really supposed to be binary
        • or porting to QUtf8String(View), if the data is supposed to be a string
        • or porting to QAnyString(View), if the overload was part of an all-stringish overload set
      2. QByteArray(View) function arguments need to decide what they represent
        • binary? Then ok
        • UTF-8 strings? Then port to QUtf8String(View)!

      This very probably is as much work as it sounds.

      Come Qt 7, we could go all-in and have

      value_type owning string class view class remarks
      char not supported, except as legacy
      QLatin1String QLatin1StringView could have qcharl1_t
      char8_t QUtf8String QUtf8StringView  
      char16_t QString QStringView maybe make QChar a namespace?
      char32_t QUtf32String QUtf32StringView so far: Qt Policy: UTF-32 isn't supported; change?
      <any> QAnyString QAnyStringView L1, U8, U16 (U32?)
      std::byte QByteArray QByteArrayView binary only, no string-ish API


        Issue Links

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



              cnn Qt Core & Network
              mmutz Marc Mutz
              Vladimir Minenko Vladimir Minenko
              Alex Blasche Alex Blasche
              1 Vote for this issue
              2 Start watching this issue



                Gerrit Reviews

                  There are no open Gerrit changes