diff --git a/src/corelib/text/qchar.h b/src/corelib/text/qchar.h index fb358ba8be..8a616b4c73 100644 --- a/src/corelib/text/qchar.h +++ b/src/corelib/text/qchar.h @@ -42,6 +42,8 @@ #include +#include // for std::hash + QT_BEGIN_NAMESPACE @@ -664,4 +666,17 @@ Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QChar &); QT_END_NAMESPACE +namespace std { +template <> +struct hash +{ + template // for transparent constexpr tracking + constexpr size_t operator()(QT_PREPEND_NAMESPACE(QChar) c) const + noexcept(noexcept(std::hash{}(u' '))) + { + return std::hash{}(c.unicode()); + } +}; +} // namespace std + #endif // QCHAR_H diff --git a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp index fcd227f1bf..b2ec6e1c1c 100644 --- a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp +++ b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp @@ -309,6 +309,13 @@ void tst_QHashFunctions::stdHash() QCOMPARE(s.size(), 2UL); } + { + std::unordered_set s = {u'H', u'W'}; + QCOMPARE(s.size(), 2UL); + s.insert(u'H'); + QCOMPARE(s.size(), 2UL); + } + } void tst_QHashFunctions::setGlobalQHashSeed()