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

Cannot declare MetaType for a QHash/QMultiHash with key type without custom operator==

    XMLWordPrintable

Details

    • 5
    • a9d51298ae34ed05adb100983ed92bec07ce2ce5 (qt/qtbase/dev) c9c8e06189055be5597b7a108a999543d44c1e41 (qt/qtbase/6.2)
    • Team 1 Foundation_Sprint 44

    Description

      The following code does not compile in Qt 6:

      struct Empty {};
      struct NoCmpKeyHash : public QHash<Empty, int>
      {
          // Works if this is uncommented
          // bool operator==(const NoCmpKeyHash &) const = delete;
      };
      uint qHash(const Empty &) { return 0; }
      
      Q_DECLARE_METATYPE(NoCmpKeyHash);
      

      with the following output:

      ../../cmake-build/qtbase/include/QtCore/../../../../qt5/qtbase/src/corelib/tools/qhashfunctions.h: In instantiation of ‘bool qHashEquals(const T&, const T&) [with T = Empty]’:
      ../../cmake-build/qtbase/include/QtCore/../../../../qt5/qtbase/src/corelib/tools/qhash.h:607:32:   required from ‘QHashPrivate::Data<Node>::iterator QHashPrivate::Data<Node>::find(const Key&) const [with Node = QHashPrivate::Node<Empty, int>; QHashPrivate::Data<Node>::iterator = QHashPrivate::iterator<QHashPrivate::Node<Empty, int> >; QHashPrivate::Data<Node>::Key = Empty]’
      ../../cmake-build/qtbase/include/QtCore/../../../../qt5/qtbase/src/corelib/tools/qhash.h:1138:14:   required from ‘QHash<Key, T>::const_iterator QHash<Key, T>::find(const Key&) const [with Key = Empty; T = int]’
      ../../cmake-build/qtbase/include/QtCore/../../../../qt5/qtbase/src/corelib/tools/qhash.h:831:28:   required from ‘QTypeTraits::compare_eq_result_container<QHash<Key, T>, U> QHash<Key, T>::operator==(const QHash<Key, T>&) const [with U = int; Key = Empty; T = int; QTypeTraits::compare_eq_result_container<QHash<Key, T>, U> = bool]’
      ../../cmake-build/qtbase/include/QtCore/../../../../qt5/qtbase/src/corelib/kernel/qmetatype.h:2219:46:   required from ‘static bool QtPrivate::QEqualityOperatorForType<T, <anonymous> >::equals(const QtPrivate::QMetaTypeInterface*, const void*, const void*) [with T = NoCmpKeyHash; bool <anonymous> = true]’
      ../../cmake-build/qtbase/include/QtCore/../../../../qt5/qtbase/src/corelib/kernel/qmetatype.h:2340:54:   required from ‘constexpr const QtPrivate::QMetaTypeInterface QtPrivate::QMetaTypeInterfaceWrapper<NoCmpKeyHash>::metaType’
      ../../cmake-build/qtbase/include/QtCore/../../../../qt5/qtbase/src/corelib/kernel/qmetatype.h:2415:12:   required from ‘constexpr const QtPrivate::QMetaTypeInterface* QtPrivate::qMetaTypeInterfaceForType() [with T = NoCmpKeyHash]’
      ../../cmake-build/qtbase/include/QtCore/../../../../qt5/qtbase/src/corelib/kernel/qmetatype.h:2476:61:   required from ‘int qRegisterNormalizedMetaType(const QByteArray&) [with T = NoCmpKeyHash]’
      ../untitled/main.cpp:146:1:   required from here
      ../../cmake-build/qtbase/include/QtCore/../../../../qt5/qtbase/src/corelib/tools/qhashfunctions.h:209:14: error: no match foroperator==’ (operand types are ‘const Empty’ and ‘const Empty’)
        209 |     return a == b;
            |            ~~^~~~
      

      Note, that explicitly deleting the operator== of the key type fixes the compilation error.

      The similar code for QMultiHash also fails in the same way.

      See also tst_collections.
       

      Attachments

        Issue Links

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

          Activity

            People

              sonakur Sona Kurazyan
              sonakur Sona Kurazyan
              Vladimir Minenko Vladimir Minenko
              Alex Blasche Alex Blasche
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes