From 5dab710b90e7041aaa9fcf3631f2ca6af1baab5f Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Fri, 27 Nov 2020 17:31:03 +0100 Subject: [PATCH] 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 Reviewed-by: Lars Knoll Reviewed-by: Andrei Golubev --- src/corelib/tools/qhashfunctions.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/corelib/tools/qhashfunctions.h b/src/corelib/tools/qhashfunctions.h index 9cfa852ba2..a03f659b52 100644 --- a/src/corelib/tools/qhashfunctions.h +++ b/src/corelib/tools/qhashfunctions.h @@ -90,6 +90,14 @@ Q_DECL_CONST_FUNCTION constexpr size_t hash(size_t key, size_t seed) noexcept } } +template +constexpr inline bool HasQHashSingleArgOverload = false; + +template +constexpr inline bool HasQHashSingleArgOverload())), size_t> +>> = true; + } 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); } Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) noexcept; -template inline size_t qHash(const T &t, size_t seed) - noexcept(noexcept(qHash(t))) +template , bool> = true> +size_t qHash(const T &t, size_t seed) noexcept(noexcept(qHash(t))) { return qHash(t) ^ seed; } namespace QtPrivate {