From 92637e93ed8cc9c607a6b544ed5944530ee99b03 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Thu, 15 Dec 2011 22:15:21 +0000 Subject: [PATCH] re PR libstdc++/51558 (Declaration of unspecialized std::hash<_Tp>::operator()(_Tp) turns compile-time errors into link-time errors) 2011-12-15 Paolo Carlini Jonathan Wakely PR libstdc++/51558 * include/bits/functional_hash.h (struct hash): Add static_assert. * src/compatibility-c++0x.cc: Adjust compatibility definitions. * testsuite/23_containers/unordered_map/erase/51142.cc: Adjust. * testsuite/23_containers/unordered_set/erase/51142.cc: Likewise. * testsuite/23_containers/unordered_multimap/erase/51142.cc: Likewise. * testsuite/23_containers/unordered_multiset/erase/51142.cc: Likewise. Co-Authored-By: Jonathan Wakely From-SVN: r182392 --- libstdc++-v3/ChangeLog | 11 ++++ libstdc++-v3/include/bits/functional_hash.h | 5 +- libstdc++-v3/src/compatibility-c++0x.cc | 58 +++++++++++++------ .../unordered_map/erase/51142.cc | 7 ++- .../unordered_multimap/erase/51142.cc | 7 ++- .../unordered_multiset/erase/51142.cc | 7 ++- .../unordered_set/erase/51142.cc | 7 ++- 7 files changed, 75 insertions(+), 27 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 38e0ff229ce..f050ad903c0 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,14 @@ +2011-12-15 Paolo Carlini + Jonathan Wakely + + PR libstdc++/51558 + * include/bits/functional_hash.h (struct hash): Add static_assert. + * src/compatibility-c++0x.cc: Adjust compatibility definitions. + * testsuite/23_containers/unordered_map/erase/51142.cc: Adjust. + * testsuite/23_containers/unordered_set/erase/51142.cc: Likewise. + * testsuite/23_containers/unordered_multimap/erase/51142.cc: Likewise. + * testsuite/23_containers/unordered_multiset/erase/51142.cc: Likewise. + 2011-12-15 Benjamin Kosnik * testsuite/22_locale/num_put/put/char/9780-2.cc: Add test for "C" diff --git a/libstdc++-v3/include/bits/functional_hash.h b/libstdc++-v3/include/bits/functional_hash.h index 2b82b21f716..e892159d449 100644 --- a/libstdc++-v3/include/bits/functional_hash.h +++ b/libstdc++-v3/include/bits/functional_hash.h @@ -57,8 +57,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template struct hash : public __hash_base { - size_t - operator()(_Tp __val) const; + static_assert(sizeof(_Tp) < 0, + "std::hash is not specialized for this type"); + size_t operator()(const _Tp&) const noexcept; }; /// Partial specializations for pointer types. diff --git a/libstdc++-v3/src/compatibility-c++0x.cc b/libstdc++-v3/src/compatibility-c++0x.cc index c5e1db092a5..03c58d244f2 100644 --- a/libstdc++-v3/src/compatibility-c++0x.cc +++ b/libstdc++-v3/src/compatibility-c++0x.cc @@ -52,36 +52,60 @@ namespace std _GLIBCXX_VISIBILITY(default) #ifndef _GLIBCXX_LONG_DOUBLE_COMPAT_IMPL template<> - size_t - hash::operator()(string __s) const - { return _Hash_impl::hash(__s.data(), __s.length()); } + struct hash + { + size_t operator()(string) const; + }; + + size_t + hash::operator()(string __s) const + { return _Hash_impl::hash(__s.data(), __s.length()); } template<> - size_t - hash::operator()(const string& __s) const - { return _Hash_impl::hash(__s.data(), __s.length()); } + struct hash + { + size_t operator()(const string&) const; + }; + + size_t + hash::operator()(const string& __s) const + { return _Hash_impl::hash(__s.data(), __s.length()); } #ifdef _GLIBCXX_USE_WCHAR_T template<> - size_t - hash::operator()(wstring __s) const - { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(wchar_t)); } + struct hash + { + size_t operator()(wstring) const; + }; + + size_t + hash::operator()(wstring __s) const + { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(wchar_t)); } template<> - size_t - hash::operator()(const wstring& __s) const - { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(wchar_t)); } + struct hash + { + size_t operator()(const wstring&) const; + }; + + size_t + hash::operator()(const wstring& __s) const + { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(wchar_t)); } #endif #endif template<> - size_t - hash::operator()(error_code __e) const + struct hash { - const size_t __tmp = std::_Hash_impl::hash(__e._M_value); - return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp); - } + size_t operator()(error_code) const; + }; + size_t + hash::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); + } // gcc-4.7.0 // changes is_monotonic to is_steady. diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/erase/51142.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/erase/51142.cc index eab637df235..7986fb23a36 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_map/erase/51142.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/erase/51142.cc @@ -27,12 +27,15 @@ struct X X(T&) {} }; +struct X_hash +{ std::size_t operator()(const X&) const { return 0; } }; + bool operator==(const X&, const X&) { return false; } // LWG 2059. -void erasor(std::unordered_map& s, X x) +void erasor(std::unordered_map& s, X x) { - std::unordered_map::iterator it = s.find(x); + std::unordered_map::iterator it = s.find(x); if (it != s.end()) s.erase(it); } diff --git a/libstdc++-v3/testsuite/23_containers/unordered_multimap/erase/51142.cc b/libstdc++-v3/testsuite/23_containers/unordered_multimap/erase/51142.cc index 678aa5dd989..0d434ac8a68 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_multimap/erase/51142.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_multimap/erase/51142.cc @@ -27,12 +27,15 @@ struct X X(T&) {} }; +struct X_hash +{ std::size_t operator()(const X&) const { return 0; } }; + bool operator==(const X&, const X&) { return false; } // LWG 2059. -void erasor(std::unordered_multimap& s, X x) +void erasor(std::unordered_multimap& s, X x) { - std::unordered_multimap::iterator it = s.find(x); + std::unordered_multimap::iterator it = s.find(x); if (it != s.end()) s.erase(it); } diff --git a/libstdc++-v3/testsuite/23_containers/unordered_multiset/erase/51142.cc b/libstdc++-v3/testsuite/23_containers/unordered_multiset/erase/51142.cc index 4db6af0fa25..7a0a1835244 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_multiset/erase/51142.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_multiset/erase/51142.cc @@ -27,12 +27,15 @@ struct X X(T&) {} }; +struct X_hash +{ std::size_t operator()(const X&) const { return 0; } }; + bool operator==(const X&, const X&) { return false; } // LWG 2059. -void erasor(std::unordered_multiset& s, X x) +void erasor(std::unordered_multiset& s, X x) { - std::unordered_multiset::iterator it = s.find(x); + std::unordered_multiset::iterator it = s.find(x); if (it != s.end()) s.erase(it); } diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/erase/51142.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/erase/51142.cc index 14864604289..ec5aeb1a234 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_set/erase/51142.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_set/erase/51142.cc @@ -27,12 +27,15 @@ struct X X(T&) {} }; +struct X_hash +{ std::size_t operator()(const X&) const { return 0; } }; + bool operator==(const X&, const X&) { return false; } // LWG 2059. -void erasor(std::unordered_set& s, X x) +void erasor(std::unordered_set& s, X x) { - std::unordered_set::iterator it = s.find(x); + std::unordered_set::iterator it = s.find(x); if (it != s.end()) s.erase(it); } -- 2.30.2