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

qHash doesn't work for nested std::pair

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.11.3
    • None
    • gcc 9.2.1
    • Linux/X11

    Description

      Hello,

      as the title says, qHash fails to work with nested std::pair. Given this code:

      #include <QHash>
      #include <utility>
      using namespace std;
      int main()
      {
        auto p{make_pair(1, make_pair(2, 3))};
        auto q{qMakePair(1, qMakePair(2, 3))};
        ::qHash(p);
        return 0;
      }
      

      I get an error on qHash(p) saying there is no candidate for qHash(T) with T=std::pair<int, int>, ie. the "inner" pair. However, qHash(q) is without problems.

      Cause

      The reason for this is that name lookup during the instantiation of qHash(const std::pair<T1, T2>&, uint) can't find the same function, either in the noexcept clause or in QtPrivate::QHashCombine::operator()(..., std::pair<A, std::pair<B, C>>) which comes earlier in the header file and is needed for instantiation of qHash(p). This is because normal lookup only sees declarations before the current declaration and the qHash<std::pair> overload comes after QtPrivate::QHashCombine. And Koenig lookup can't find that overload, because it is i Qt's (or global) namespace, but it would need to be in std namespace.

      Solution

      The best solution would be to move qHash(const std::pair<T1, T2> &key, uint seed = 0) out of QT_BEGIN_NAMESPACE into the std namespace. This allows it to work without problems using Koenig lookup.

      Note that if future Qt versions use std::pair as QPair, this problem should be considered for them as well.

      Attachments

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

        Activity

          People

            mmutz Marc Mutz
            jpalecek Jiří Paleček
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes