From: Jonathan Wakely Date: Wed, 20 Sep 2017 13:24:45 +0000 (+0100) Subject: PR libstdc++/82262 fix std::hash> X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f6b05c44aad49d8c37f4b3deaac93565126340f9;p=gcc.git PR libstdc++/82262 fix std::hash> 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. From-SVN: r253010 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index f9424a58c8a..6c92731ae6a 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2017-09-20 Jonathan Wakely + + 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. + 2017-09-19 Jonathan Wakely PR libstdc++/71500 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>::__enable_hash_call> + template, + 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(-3333); - return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash; + return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash; } }; - template - struct __optional_hash_call_base<_Tp, false> {}; + template + struct __optional_hash_call_base<_Tp, _Up, false> {}; template struct hash> 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 auto f(...) -> decltype(std::false_type()); static_assert(!decltype(f(0))::value, ""); -static_assert(!std::is_invocable_v< - std::hash>&, std::optional const&> ); -static_assert(std::is_invocable_v< - std::hash>&, std::optional const&> ); + +template +constexpr bool hashable() +{ return std::is_invocable_v&, const T&>; } + +static_assert(!hashable>()); +static_assert(!hashable>()); +static_assert(hashable>()); +static_assert(hashable>()); int main() { int x = 42; std::optional x2 = 42; VERIFY(std::hash()(x) == std::hash>()(x2)); + + // PR libstdc++/82262 + std::optional x3 = x2; + VERIFY(std::hash()(x) == std::hash>()(x3)); }