Seedless qHash catch-all: make it SFINAE-friendly

To support qHash overloads without a seed we have a qHash(T, size_t)
catch-all that calls qHash(T) and XORs the seed. The problem is
that this catch-all is not SFINAE friendly. For a type Foo which
does not have any qHash overload, we can't ask if qHash(Foo, size_t)
is callable because it would instantiate the catch-all and fail
to compile.

Add a suitable trait and enable_if.

Pick-to: 6.0
Change-Id: Idffd48a537eebaf77cee7030b8d91a302643ffde
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2020-11-27 17:31:03 +01:00
parent d982fdfca4
commit 5dab710b90

View File

@ -90,6 +90,14 @@ Q_DECL_CONST_FUNCTION constexpr size_t hash(size_t key, size_t seed) noexcept
} }
} }
template <typename T, typename = void>
constexpr inline bool HasQHashSingleArgOverload = false;
template <typename T>
constexpr inline bool HasQHashSingleArgOverload<T, std::enable_if_t<
std::is_convertible_v<decltype(qHash(std::declval<const T &>())), size_t>
>> = true;
} }
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHashBits(const void *p, size_t size, size_t seed = 0) noexcept; Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHashBits(const void *p, size_t size, size_t seed = 0) noexcept;
@ -166,8 +174,8 @@ Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(QKeyCombination key, size_t
{ return qHash(key.toCombined(), seed); } { return qHash(key.toCombined(), seed); }
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) noexcept; Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) noexcept;
template<typename T> inline size_t qHash(const T &t, size_t seed) template <typename T, std::enable_if_t<QHashPrivate::HasQHashSingleArgOverload<T>, bool> = true>
noexcept(noexcept(qHash(t))) size_t qHash(const T &t, size_t seed) noexcept(noexcept(qHash(t)))
{ return qHash(t) ^ seed; } { return qHash(t) ^ seed; }
namespace QtPrivate { namespace QtPrivate {