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

As a user of both Qt and C++ container classes, I wish Qt would finally fix the broken insert() behavior in its associative containers




      While I like the STL containers, and use them every day, I also like the new QHash, which I'm using wherever possible instead of std::unordered_map. However, something that continues to bug me is their incompatible insertion behaviour. I was very disappointed when I learned that this still isn't fixed in Qt 6: An STL associative container will do nothing in insert() if the key already exists, reporting that clearly by result.second. Qt containers, otoh, overwrite existing values. And while STL containers allow me to choose the insertion behaviour (insert() vs. insert_or_assign()), there simply is no API in Qt containers that would allow me to get STL-compatible insertion behaviour. The only way I have found is to use the size-check-trick, first seen in the old implementation of QStringList::removeDuplicates() (now incorporated into QDuplicateTracker):

      const auto oldSize = c.size();
      auto &e = c[key];
      if (c.size() != oldSize) {
          // added

      One always has to write a comment when using that pattern, because it's so convoluted.

      While the other glaring incompatibility, decltype(*it) is caught by the compiler, the broken {{insert()}}ion behaviour is not, which makes this very tricky to fix, I concur. But fix it you must, this behaviour is really annoying if one pays attention to avoid double lookups.

      Please come up with a strategy to fix this, finally.

      Oh, and also the inverted insertion order in QMultiMap and QMultiHash.


        For Gerrit Dashboard: QTBUG-99651
        # Subject Branch Project Status CR V



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



                Gerrit Reviews

                There are no open Gerrit changes