Provide qHash for all C++ fundamental types

Except for (void and) bool, which we may not want to have
to avoid accidental implicit conversions.
Drive-by, rearrange qHash overloads to C++ types first,
and then Qt ones.

Change-Id: I9c4ecef5f28568d35ca52e339583851ce53b3bae
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
bb10
Giuseppe D'Angelo 2020-02-16 13:42:19 +01:00
parent 657fa0462d
commit b4a05b1233
2 changed files with 56 additions and 9 deletions

View File

@ -769,6 +769,34 @@ uint qt_hash(QStringView key, uint chained) noexcept
Returns the hash value for the \a key, using \a seed to seed the calculation.
*/
/*! \fn size_t qHash(char8_t key, size_t seed = 0)
\relates QHash
\since 6.0
Returns the hash value for the \a key, using \a seed to seed the calculation.
*/
/*! \fn size_t qHash(char16_t key, size_t seed = 0)
\relates QHash
\since 6.0
Returns the hash value for the \a key, using \a seed to seed the calculation.
*/
/*! \fn size_t qHash(char32_t key, size_t seed = 0)
\relates QHash
\since 6.0
Returns the hash value for the \a key, using \a seed to seed the calculation.
*/
/*! \fn size_t qHash(wchar_t key, size_t seed = 0)
\relates QHash
\since 6.0
Returns the hash value for the \a key, using \a seed to seed the calculation.
*/
/*! \fn size_t qHash(float key, size_t seed) noexcept
\relates QHash
\since 5.3
@ -870,6 +898,13 @@ size_t qHash(long double key, size_t seed) noexcept
Returns the hash value for the \a key, using \a seed to seed the calculation.
*/
/*! \fn template <class T> size_t qHash(std::nullptr_t key, size_t seed = 0)
\relates QHash
\since 6.0
Returns the hash value for the \a key, using \a seed to seed the calculation.
*/
/*!
\class QHash
\inmodule QtCore

View File

@ -95,6 +95,7 @@ Q_DECL_CONST_FUNCTION constexpr size_t hash(size_t key, size_t seed) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHashBits(const void *p, size_t size, size_t seed = 0) noexcept;
// C++ builtin types
Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(char key, size_t seed = 0) noexcept
{ return QHashPrivate::hash(size_t(key), seed); }
Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(uchar key, size_t seed = 0) noexcept
@ -132,6 +133,26 @@ Q_CORE_EXPORT Q_DECL_CONST_FUNCTION size_t qHash(double key, size_t seed = 0) no
#if !defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION size_t qHash(long double key, size_t seed = 0) noexcept;
#endif
Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(wchar_t key, size_t seed = 0) noexcept
{ return QHashPrivate::hash(size_t(key), seed); }
Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(char16_t key, size_t seed = 0) noexcept
{ return QHashPrivate::hash(size_t(key), seed); }
Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(char32_t key, size_t seed = 0) noexcept
{ return QHashPrivate::hash(size_t(key), seed); }
#ifdef __cpp_char8_t
Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline size_t qHash(char8_t key, size_t seed = 0) noexcept
{ return QHashPrivate::hash(size_t(key), seed); }
#endif
template <class T> inline size_t qHash(const T *key, size_t seed = 0) noexcept
{
return qHash(reinterpret_cast<quintptr>(key), seed);
}
Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(std::nullptr_t, size_t seed = 0) noexcept
{
return seed;
}
// (some) Qt types
Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(const QChar key, size_t seed = 0) noexcept { return qHash(key.unicode(), seed); }
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QByteArray &key, size_t seed = 0) noexcept;
#if QT_STRINGVIEW_LEVEL < 2
@ -143,15 +164,6 @@ Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QBitArray &key, size_t see
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(QLatin1String key, size_t seed = 0) noexcept;
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) noexcept;
Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(std::nullptr_t, size_t seed = 0) noexcept
{
return seed;
}
template <class T> inline size_t qHash(const T *key, size_t seed = 0) noexcept
{
return qHash(reinterpret_cast<quintptr>(key), seed);
}
template<typename T> inline size_t qHash(const T &t, size_t seed)
noexcept(noexcept(qHash(t)))
{ return qHash(t) ^ seed; }