[PATCH] libstdc++: Value-initialize hashtable helper objects
Marco Falke <[email protected]> Sat, 1 Aug 2026 08:50:17 +0200
| Newsgroups | gmane.comp.gcc.patches |
|---|---|
| Message-ID | <CAK51vgCM2tNRO=ktU-nt66FD7mcrAE0g+Pq=kkeZ3n1p+GDOpg@mail.gmail.com> |
libstdc++15 and 16 do not allow to construct an unordered set with a
hasher whose ctor is explicit.
This is an issue with Clang, or with GCC -Wsystem-headers. See
https://godbolt.org/z/EGzKbjh84
Fix this by using value-init, similar to commit
f8f0193b5b83f6e85d65015e79c803295baf5166. However, that likely
regressed again in commit 689d4abc0b836aaf1c8ecd38091dde6b14373c60?
patch is attached and copied later.
I'd say the unit test could be expanded by adding
// { dg-additional-options "-Werror -Wsystem-headers" }
, but I wasn't sure if this is recommended.
From d94f4ef24d90d81d6e5dd6e9807957f398d7160e Mon Sep 17 00:00:00 2001
From: MarcoFalke <[email protected]>
Date: Sun, 26 Jul 2026 14:30:58 +0000
Subject: [PATCH] libstdc++: Value-initialize hashtable helper objects
libstdc++-v3/ChangeLog:
* include/bits/hashtable_policy.h (_Hashtable_ebo_helper):
Value-initialize the stored object.
* testsuite/23_containers/unordered_set/cons/explicit.cc: New test.
---
libstdc++-v3/include/bits/hashtable_policy.h | 4 ++--
.../unordered_set/cons/explicit.cc | 17 +++++++++++++++++
2 files changed, 19 insertions(+), 2 deletions(-)
create mode 100644
libstdc++-v3/testsuite/23_containers/unordered_set/cons/explicit.cc
diff --git a/libstdc++-v3/include/bits/hashtable_policy.h
b/libstdc++-v3/include/bits/hashtable_policy.h
index 6e4b365d592..1f1bf0bb111 100644
--- a/libstdc++-v3/include/bits/hashtable_policy.h
+++ b/libstdc++-v3/include/bits/hashtable_policy.h
@@ -1033,7 +1033,7 @@ namespace __detail
bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
struct _Hashtable_ebo_helper
{
- [[__no_unique_address__]] _Tp _M_obj;
+ [[__no_unique_address__]] _Tp _M_obj{};
};
#if ! _GLIBCXX_INLINE_VERSION
@@ -1042,7 +1042,7 @@ namespace __detail
template<typename _Tp>
struct _Hashtable_ebo_helper<_Tp, false>
{
- _Tp _M_obj;
+ _Tp _M_obj{};
};
#endif
diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/cons/explicit.cc
b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/explicit.cc
new file mode 100644
index 00000000000..9a358e6debd
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/explicit.cc
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++11 } }
+
+#include <unordered_set>
+
+struct Hash
+{
+ explicit Hash(bool = false) { }
+
+ std::size_t operator()(int value) const
+ { return value; }
+};
+
+void
+test01()
+{
+ std::unordered_set<int, Hash> set;
+}
--
2.53.0
0001-libstdc-Value-initialize-hashtable-helper-objects.patch
(text/x-patch, 1.9 KB)
From d94f4ef24d90d81d6e5dd6e9807957f398d7160e Mon Sep 17 00:00:00 2001 From: MarcoFalke <[email protected]> Date: Sun, 26 Jul 2026 14:30:58 +0000 Subject: [PATCH] libstdc++: Value-initialize hashtable helper objects libstdc++-v3/ChangeLog: * include/bits/hashtable_policy.h (_Hashtable_ebo_helper): Value-initialize the stored object. * testsuite/23_containers/unordered_set/cons/explicit.cc: New test. --- libstdc++-v3/include/bits/hashtable_policy.h | 4 ++-- .../unordered_set/cons/explicit.cc | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/unordered_set/cons/explicit.cc diff --git a/libstdc++-v3/include/bits/hashtable_policy.h b/libstdc++-v3/include/bits/hashtable_policy.h index 6e4b365d592..1f1bf0bb111 100644 --- a/libstdc++-v3/include/bits/hashtable_policy.h +++ b/libstdc++-v3/include/bits/hashtable_policy.h @@ -1033,7 +1033,7 @@ namespace __detail bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)> struct _Hashtable_ebo_helper { - [[__no_unique_address__]] _Tp _M_obj; + [[__no_unique_address__]] _Tp _M_obj{}; }; #if ! _GLIBCXX_INLINE_VERSION @@ -1042,7 +1042,7 @@ namespace __detail template<typename _Tp> struct _Hashtable_ebo_helper<_Tp, false> { - _Tp _M_obj; + _Tp _M_obj{}; }; #endif diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/cons/explicit.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/explicit.cc new file mode 100644 index 00000000000..9a358e6debd --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/explicit.cc @@ -0,0 +1,17 @@ +// { dg-do compile { target c++11 } } + +#include <unordered_set> + +struct Hash +{ + explicit Hash(bool = false) { } + + std::size_t operator()(int value) const + { return value; } +}; + +void +test01() +{ + std::unordered_set<int, Hash> set; +} -- 2.53.0