public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r15-1256] libstdc++: Improve diagnostics for invalid std::hash specializations [PR115420]
@ 2024-06-13 13:10 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2024-06-13 13:10 UTC (permalink / raw)
To: gcc-cvs, libstdc++-cvs
https://gcc.gnu.org/g:cc38bdf093c44918edff819ae6c73d03c726b341
commit r15-1256-gcc38bdf093c44918edff819ae6c73d03c726b341
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Jun 11 11:08:12 2024 +0100
libstdc++: Improve diagnostics for invalid std::hash specializations [PR115420]
When using a key type without a valid std::hash specialization the
unordered containers give confusing diagnostics about the default
constructor being deleted. Add a static_assert that will fail for
disabled std::hash specializations (and for a subset of custom hash
functions).
libstdc++-v3/ChangeLog:
PR libstdc++/115420
* include/bits/hashtable.h (_Hashtable): Add static_assert to
check that hash function is copy constructible.
* testsuite/23_containers/unordered_map/115420.cc: New test.
Diff:
---
libstdc++-v3/include/bits/hashtable.h | 2 ++
.../testsuite/23_containers/unordered_map/115420.cc | 16 ++++++++++++++++
2 files changed, 18 insertions(+)
diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h
index 983aa909d6c7..361da2b3b4d4 100644
--- a/libstdc++-v3/include/bits/hashtable.h
+++ b/libstdc++-v3/include/bits/hashtable.h
@@ -210,6 +210,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
static_assert(is_same<typename _Alloc::value_type, _Value>{},
"unordered container must have the same value_type as its allocator");
#endif
+ static_assert(is_copy_constructible<_Hash>::value,
+ "hash function must be copy constructible");
using __traits_type = _Traits;
using __hash_cached = typename __traits_type::__hash_cached;
diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/115420.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/115420.cc
new file mode 100644
index 000000000000..5528bf813bd7
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/unordered_map/115420.cc
@@ -0,0 +1,16 @@
+// { dg-do compile { target c++11 } }
+
+#include <unordered_map>
+
+struct S { };
+
+void
+test_pr115420()
+{
+ std::unordered_map<S, int> m; // { dg-error "here" }
+}
+
+// { dg-error "hash function must be copy constructible" "" { target *-*-* } 0 }
+// { dg-prune-output "use of deleted function" }
+// { dg-prune-output "is private" }
+// { dg-prune-output "no matching function" }
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2024-06-13 13:10 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-13 13:10 [gcc r15-1256] libstdc++: Improve diagnostics for invalid std::hash specializations [PR115420] Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).