* [PATCH] PR libstdc++/82262 fix std::hash<std::optional<const T>>
@ 2017-09-20 13:24 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2017-09-20 13:24 UTC (permalink / raw)
To: libstdc++, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 400 bytes --]
The std::hash specialization for std::optional was missing a
remove_const_t so tried to use a disabled hash specialization.
PR libstdc++/82262
* include/std/optional (__optional_hash_call_base): Add template
parameter for remove_const_t<_Tp> and use it consistently.
* testsuite/20_util/optional/hash.cc: Test optional<const T>.
Tested powerpc64le-linux, committing to trunk and gcc-7-branch.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2950 bytes --]
commit 04f9d0dcc8c25bef1b5a7adb6e916390da1a7a89
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Wed Sep 20 14:08:30 2017 +0100
PR libstdc++/82262 fix std::hash<std::optional<const T>>
PR libstdc++/82262
* include/std/optional (__optional_hash_call_base): Add template
parameter for remove_const_t<_Tp> and use it consistently.
* testsuite/20_util/optional/hash.cc: Test optional<const T>.
diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional
index 2743ef963b4..2df9b5443ef 100644
--- a/libstdc++-v3/include/std/optional
+++ b/libstdc++-v3/include/std/optional
@@ -1005,23 +1005,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Hash.
- template<typename _Tp, bool
- = __poison_hash<remove_const_t<_Tp>>::__enable_hash_call>
+ template<typename _Tp, typename _Up = remove_const_t<_Tp>,
+ bool = __poison_hash<_Up>::__enable_hash_call>
struct __optional_hash_call_base
{
size_t
operator()(const optional<_Tp>& __t) const
- noexcept(noexcept(hash<_Tp> {}(*__t)))
+ noexcept(noexcept(hash<_Up>{}(*__t)))
{
// We pick an arbitrary hash for disengaged optionals which hopefully
// usual values of _Tp won't typically hash to.
constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
- return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
+ return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash;
}
};
- template<typename _Tp>
- struct __optional_hash_call_base<_Tp, false> {};
+ template<typename _Tp, typename _Up>
+ struct __optional_hash_call_base<_Tp, _Up, false> {};
template<typename _Tp>
struct hash<optional<_Tp>>
diff --git a/libstdc++-v3/testsuite/20_util/optional/hash.cc b/libstdc++-v3/testsuite/20_util/optional/hash.cc
index c16f0b20fb2..35ae51b947a 100644
--- a/libstdc++-v3/testsuite/20_util/optional/hash.cc
+++ b/libstdc++-v3/testsuite/20_util/optional/hash.cc
@@ -29,14 +29,23 @@ template<class T>
auto f(...) -> decltype(std::false_type());
static_assert(!decltype(f<S>(0))::value, "");
-static_assert(!std::is_invocable_v<
- std::hash<std::optional<S>>&, std::optional<S> const&> );
-static_assert(std::is_invocable_v<
- std::hash<std::optional<int>>&, std::optional<int> const&> );
+
+template<typename T>
+constexpr bool hashable()
+{ return std::is_invocable_v<std::hash<T>&, const T&>; }
+
+static_assert(!hashable<std::optional<S>>());
+static_assert(!hashable<std::optional<const S>>());
+static_assert(hashable<std::optional<int>>());
+static_assert(hashable<std::optional<const int>>());
int main()
{
int x = 42;
std::optional<int> x2 = 42;
VERIFY(std::hash<int>()(x) == std::hash<std::optional<int>>()(x2));
+
+ // PR libstdc++/82262
+ std::optional<const int> x3 = x2;
+ VERIFY(std::hash<int>()(x) == std::hash<std::optional<const int>>()(x3));
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2017-09-20 13:24 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-20 13:24 [PATCH] PR libstdc++/82262 fix std::hash<std::optional<const T>> Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).