diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2019-05-04 15:35:25 +0100 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2019-05-04 15:35:25 +0100 |
commit | fe6fb0d1590237c67b75342dc41b7ffdbdcccf25 (patch) | |
tree | dbd474a3076c23ef0d047655b62b3ff91992dbd0 | |
parent | 34d9c2c269c553a1f15813f6d7624e8aca87adef (diff) | |
download | gcc-fe6fb0d1590237c67b75342dc41b7ffdbdcccf25.zip gcc-fe6fb0d1590237c67b75342dc41b7ffdbdcccf25.tar.gz gcc-fe6fb0d1590237c67b75342dc41b7ffdbdcccf25.tar.bz2 |
Fix std::hash<std::error_condition>
The hash value should be based on the identity (i.e. address) of the
error_category member, not its object representation (i.e. underlying
bytes).
* include/std/system_error (error_code): Remove friend declaration
for hash<error_code>.
(hash<error_code>::operator()): Use public member functions to access
value and category.
(hash<error_condition>::operator()): Use address of category, not
its object representation.
* src/c++11/compatibility-c++0x.cc (hash<error_code>::operator()):
Use public member functions to access value and category.
* testsuite/19_diagnostics/error_condition/hash.cc: New test.
From-SVN: r270872
-rw-r--r-- | libstdc++-v3/ChangeLog | 12 | ||||
-rw-r--r-- | libstdc++-v3/include/std/system_error | 10 | ||||
-rw-r--r-- | libstdc++-v3/src/c++11/compatibility-c++0x.cc | 4 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/19_diagnostics/error_condition/hash.cc | 51 |
4 files changed, 69 insertions, 8 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index c434fbc..9965009 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2019-05-04 Jonathan Wakely <jwakely@redhat.com> + + * include/std/system_error (error_code): Remove friend declaration + for hash<error_code>. + (hash<error_code>::operator()): Use public member functions to access + value and category. + (hash<error_condition>::operator()): Use address of category, not + its object representation. + * src/c++11/compatibility-c++0x.cc (hash<error_code>::operator()): + Use public member functions to access value and category. + * testsuite/19_diagnostics/error_condition/hash.cc: New test. + 2019-05-04 François Dumont <fdumont@gcc.gnu.org> PR libstdc++/90277 diff --git a/libstdc++-v3/include/std/system_error b/libstdc++-v3/include/std/system_error index b7891cb..a60c96a 100644 --- a/libstdc++-v3/include/std/system_error +++ b/libstdc++-v3/include/std/system_error @@ -193,8 +193,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // DR 804. private: - friend class hash<error_code>; - int _M_value; const error_category* _M_cat; }; @@ -394,13 +392,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION size_t operator()(const error_code& __e) const noexcept { - const size_t __tmp = std::_Hash_impl::hash(__e._M_value); - return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp); + const size_t __tmp = std::_Hash_impl::hash(__e.value()); + return std::_Hash_impl::__hash_combine(&__e.category(), __tmp); } }; #endif // _GLIBCXX_COMPATIBILITY_CXX0X -#if __cplusplus > 201402L +#if __cplusplus >= 201703L // DR 2686. /// std::hash specialization for error_condition. template<> @@ -411,7 +409,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator()(const error_condition& __e) const noexcept { const size_t __tmp = std::_Hash_impl::hash(__e.value()); - return std::_Hash_impl::__hash_combine(__e.category(), __tmp); + return std::_Hash_impl::__hash_combine(&__e.category(), __tmp); } }; #endif diff --git a/libstdc++-v3/src/c++11/compatibility-c++0x.cc b/libstdc++-v3/src/c++11/compatibility-c++0x.cc index d57fdd2..25ebe43 100644 --- a/libstdc++-v3/src/c++11/compatibility-c++0x.cc +++ b/libstdc++-v3/src/c++11/compatibility-c++0x.cc @@ -117,8 +117,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION size_t hash<error_code>::operator()(error_code __e) const { - const size_t __tmp = std::_Hash_impl::hash(__e._M_value); - return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp); + const size_t __tmp = std::_Hash_impl::hash(__e.value()); + return std::_Hash_impl::__hash_combine(&__e.category(), __tmp); } // gcc-4.7.0 diff --git a/libstdc++-v3/testsuite/19_diagnostics/error_condition/hash.cc b/libstdc++-v3/testsuite/19_diagnostics/error_condition/hash.cc new file mode 100644 index 0000000..3bc0561 --- /dev/null +++ b/libstdc++-v3/testsuite/19_diagnostics/error_condition/hash.cc @@ -0,0 +1,51 @@ +// 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 +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } + +#include <system_error> +#include <testsuite_hooks.h> + +struct error_cat : std::error_category +{ + error_cat(std::string s) : _name(s) { } + std::string _name; + const char* name() const noexcept override { return _name.c_str(); } + std::string message(int) const override { return "error"; } +}; + +void +test01() +{ + std::hash<std::error_condition> h; + error_cat kitty("kitty"), moggy("moggy"); + std::error_condition cond1(99, kitty); + VERIFY( h(cond1) == h(cond1) ); + std::error_condition cond2(99, kitty); + VERIFY( h(cond1) == h(cond2) ); + std::error_condition cond3(88, kitty); + VERIFY( h(cond1) != h(cond3) ); + std::error_condition cond4(99, moggy); + VERIFY( h(cond1) != h(cond4) ); +} + +int +main() +{ + test01(); +} |