summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2020-05-04 18:13:28 +0200
committerMarc Mutz <marc.mutz@kdab.com>2020-05-05 18:58:26 +0200
commit900e8b023bccaba6b200b91e2ca5f0d0203e1eed (patch)
treee69948d1307553f3db292a63d0e270989a7bdb4c
parent0bed456f47d9c13d0158ee336d4a13bcf3a3bda3 (diff)
QChar: make std::hash'able
Change-Id: I2164df19cd17ab96a39020de66a3fe9fec838a36 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
-rw-r--r--src/corelib/text/qchar.h15
-rw-r--r--tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp7
2 files changed, 22 insertions, 0 deletions
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 <QtCore/qglobal.h>
+#include <functional> // 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<QT_PREPEND_NAMESPACE(QChar)>
+{
+ template <typename = void> // for transparent constexpr tracking
+ constexpr size_t operator()(QT_PREPEND_NAMESPACE(QChar) c) const
+ noexcept(noexcept(std::hash<char16_t>{}(u' ')))
+ {
+ return std::hash<char16_t>{}(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<QChar> s = {u'H', u'W'};
+ QCOMPARE(s.size(), 2UL);
+ s.insert(u'H');
+ QCOMPARE(s.size(), 2UL);
+ }
+
}
void tst_QHashFunctions::setGlobalQHashSeed()