diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-11-27 17:31:03 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-11-30 17:16:21 +0100 |
commit | 5dab710b90e7041aaa9fcf3631f2ca6af1baab5f (patch) | |
tree | 5abed1af0e0b3f7bb71328c96dcbfbd9dd6da5e7 /src/corelib/tools | |
parent | d982fdfca43165b31db7f7c3602b93f7197aeee8 (diff) |
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>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qhashfunctions.h | 12 |
1 files 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 <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; @@ -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<typename T> inline size_t qHash(const T &t, size_t seed) - noexcept(noexcept(qHash(t))) +template <typename T, std::enable_if_t<QHashPrivate::HasQHashSingleArgOverload<T>, bool> = true> +size_t qHash(const T &t, size_t seed) noexcept(noexcept(qHash(t))) { return qHash(t) ^ seed; } namespace QtPrivate { |