diff --git a/src/corelib/tools/qhashfunctions.h b/src/corelib/tools/qhashfunctions.h index 4f8e4066ee..4d0b2d9519 100644 --- a/src/corelib/tools/qhashfunctions.h +++ b/src/corelib/tools/qhashfunctions.h @@ -251,6 +251,9 @@ struct QNothrowHashable : std::false_type {}; template struct QNothrowHashable>> : std::true_type {}; +template +constexpr inline bool QNothrowHashable_v = QNothrowHashable::value; + } // namespace QtPrivate template @@ -317,15 +320,15 @@ template inline size_t qHash(const std::pair using argument_type = QT_PREPEND_NAMESPACE(Class); \ using result_type = size_t; \ size_t operator()(Arguments s) const \ - noexcept(noexcept(QT_PREPEND_NAMESPACE(qHash)(s))) \ + noexcept(QT_PREPEND_NAMESPACE( \ + QtPrivate::QNothrowHashable_v)) \ { \ /* this seeds qHash with the result of */ \ /* std::hash applied to an int, to reap */ \ /* any protection against predictable hash */ \ /* values the std implementation may provide */ \ - return QT_PREPEND_NAMESPACE(qHash)(s, \ - QT_PREPEND_NAMESPACE(qHash)( \ - std::hash{}(0))); \ + using QT_PREPEND_NAMESPACE(qHash); \ + return qHash(s, qHash(std::hash{}(0))); \ } \ }; \ } \ diff --git a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp index 86305b101f..3281c4cefd 100644 --- a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp +++ b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp @@ -303,8 +303,34 @@ void tst_QHashFunctions::rangeCommutative() } } +// QVarLengthArray these days has a qHash() as a hidden friend. +// This checks that QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH can deal with that: + +QT_BEGIN_NAMESPACE +QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QVarLengthArray>) +QT_END_NAMESPACE + void tst_QHashFunctions::stdHash() { + { + std::unordered_set>> s = { + { + {0, 1, 2}, + {42, 43, 44}, + {}, + }, { + {11, 12, 13}, + {}, + }, + }; + QCOMPARE(s.size(), 2UL); + s.insert({ + {11, 12, 13}, + {}, + }); + QCOMPARE(s.size(), 2UL); + } + { std::unordered_set s = {QStringLiteral("Hello"), QStringLiteral("World")}; QCOMPARE(s.size(), 2UL);