]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix std::unordered_set::emplace optimization [PR117686]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 19 Nov 2024 23:38:19 +0000 (23:38 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 20 Nov 2024 06:44:43 +0000 (06:44 +0000)
The __is_key_type specialization that matches a pair<key_type, T>
argument is intended for std::unordered_map, not for
std::unordered_set<std::pair<K,T>>.

This uses a pair<const Args&...> as the template argument for
__is_key_type, so that it won't match a set's key_type.

libstdc++-v3/ChangeLog:

PR libstdc++/117686
* include/bits/hashtable.h (_Hashtable::_M_emplace_uniq):
Adjust usage of __is_key_type to avoid false positive.
* testsuite/23_containers/unordered_set/insert/117686.cc:
New test.

libstdc++-v3/include/bits/hashtable.h
libstdc++-v3/testsuite/23_containers/unordered_set/insert/117686.cc [new file with mode: 0644]

index a704816573ae6445a6e9391fd51340073fed7f0f..b8bd8c2f41816a800bab9b3589fe609b16285ad1 100644 (file)
@@ -2286,9 +2286,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          }
        else if constexpr (sizeof...(_Args) == 2)
          {
-           pair<const _Args&...> __refs(__args...);
-           if constexpr (__is_key_type<pair<_Args...>>)
+           if constexpr (__is_key_type<pair<const _Args&...>>)
              {
+               pair<const _Args&...> __refs(__args...);
                const auto& __key = _ExtractKey{}(__refs);
                __kp = std::__addressof(__key);
              }
diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/insert/117686.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/insert/117686.cc
new file mode 100644 (file)
index 0000000..3baac16
--- /dev/null
@@ -0,0 +1,16 @@
+// { dg-do compile { target c++11 } }
+// Bug 117686 - error in unordered_set::emplace
+
+#include <unordered_set>
+#include <utility>
+
+struct H {
+  std::size_t operator()(const std::pair<int, int>&) const { return 0; }
+};
+
+void
+test_117686()
+{
+  std::unordered_set<std::pair<int, int>, H> s;
+  s.emplace(1, 2);
+}