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

Apply std::launder() where necessary

    XMLWordPrintable

Details

    • std::launder

    Description

      According to resident language lawyer villevoutilainen_qt, with C++20, the only remaining use case of std::launder() is as a de-virtualization barrier:

      class B { ~~~~ };
      class D1 : public B { ~~~~ };
      class D2 : public B { ~~~~ };
      static_assert(sizeof(D1) == sizeof(D2));
      
      void mutate(B *b) // out-of-line, so compiler doesn't see what's going on
      {
          assert(dynamic_cast<D1*>(b) || dynamic_cast<D2*>(b));
          b->~B();
          new (b) D2();
      }
      
      D1 d;
      B *b = &d;
      b->someVirtualCall();
      mutate(b);
      b->someVirtualCall(); // compiler may assume vtbl didn't change and call D1::someVirtualCall() directly
      std::launder(b)->someVirtualCall(); // OK, compiler musn't de-virtualize
      

      As for const and reference members (the C++17 case), there was only one compiler (IBM xlc++) which optimized on this, but only for static-storage duration objects. So adding it to Q_APPLICATION_STATIC was ok, as a precaution. Everything else is not needed, esp. as the aforementioned compiler became non-compliant in C++20.

      Attachments

        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:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes