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
This commit is contained in:
parent
34d9c2c269
commit
fe6fb0d159
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user