From 17a73b3c47f58155350af2e1ca359e915ffedcae Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Fri, 11 Jan 2019 14:54:49 +0000 Subject: [PATCH] PR libstdc++/88802 define std::hash for C++17 PR libstdc++/88802 * include/bits/functional_hash.h (hash): Define specialization for C++17 (P0513R0, LWG 2817). * testsuite/20_util/hash/nullptr.cc: New test. From-SVN: r267845 --- libstdc++-v3/ChangeLog | 5 +++ libstdc++-v3/include/bits/functional_hash.h | 10 +++++ .../testsuite/20_util/hash/nullptr.cc | 37 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 libstdc++-v3/testsuite/20_util/hash/nullptr.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 37bd9e2accd..4c2470c6326 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,10 @@ 2019-01-11 Jonathan Wakely + PR libstdc++/88802 + * include/bits/functional_hash.h (hash): Define + specialization for C++17 (P0513R0, LWG 2817). + * testsuite/20_util/hash/nullptr.cc: New test. + PR libstdc++/88125 * config/abi/pre/gnu.ver (GLIBCXX_3.4.6): Remove unused duplicate pattern for std::basic_stringbuf::str(). diff --git a/libstdc++-v3/include/bits/functional_hash.h b/libstdc++-v3/include/bits/functional_hash.h index 2245fad69a7..32dc00c5a6f 100644 --- a/libstdc++-v3/include/bits/functional_hash.h +++ b/libstdc++-v3/include/bits/functional_hash.h @@ -254,6 +254,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator()(long double __val) const noexcept; }; +#if __cplusplus >= 201703L + template<> + struct hash : public __hash_base + { + size_t + operator()(nullptr_t) const noexcept + { return 0; } + }; +#endif + // @} group hashes // Hint about performance of hash functor. If not fast the hash-based diff --git a/libstdc++-v3/testsuite/20_util/hash/nullptr.cc b/libstdc++-v3/testsuite/20_util/hash/nullptr.cc new file mode 100644 index 00000000000..366db679c36 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/hash/nullptr.cc @@ -0,0 +1,37 @@ +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } + +#include +#include + +void +test01() +{ + auto h1 = std::hash{}(nullptr); + static_assert(std::is_same_v); + auto h2 = std::hash{}(nullptr); + VERIFY( h1 == h2 ); +} + +int +main() +{ + test01(); +} -- 2.30.2