]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Use ADL swap for containers' function objects [PR117921]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 5 Dec 2024 12:46:26 +0000 (12:46 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 28 Feb 2025 15:27:45 +0000 (15:27 +0000)
The standard says that Compare, Pred and Hash objects should be swapped
as described in [swappable.requirements] which means calling swap
unqualified with std::swap visible to name lookup.

libstdc++-v3/ChangeLog:

PR libstdc++/117921
* include/bits/hashtable_policy.h (_Hash_code_base::_M_swap):
Use ADL swap for Hash members.
(_Hashtable_base::_M_swap): Use ADL swap for _Equal members.
* include/bits/stl_tree.h (_Rb_tree::swap): Use ADL swap for
_Compare members.
* testsuite/23_containers/set/modifiers/swap/adl.cc: New test.
* testsuite/23_containers/unordered_set/modifiers/swap-2.cc: New
test.

(cherry picked from commit 0368c42507328774cadbea589509b95aaf3cb826)

libstdc++-v3/include/bits/hashtable_policy.h
libstdc++-v3/include/bits/stl_tree.h
libstdc++-v3/testsuite/23_containers/set/modifiers/swap/adl.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/unordered_set/modifiers/swap-2.cc [new file with mode: 0644]

index 68ec8d9470a7cfe50a07b4ed7a205d5fc8ce4692..738a4da9382efc770f297363528fdb4f73ea1721 100644 (file)
@@ -1396,7 +1396,10 @@ namespace __detail
 
       void
       _M_swap(_Hash_code_base& __x)
-      { std::swap(__ebo_hash::_M_get(), __x.__ebo_hash::_M_get()); }
+      {
+       using std::swap;
+       swap(__ebo_hash::_M_get(), __x.__ebo_hash::_M_get());
+      }
 
       const _Hash&
       _M_hash() const { return __ebo_hash::_M_cget(); }
@@ -1780,7 +1783,8 @@ namespace __detail
       _M_swap(_Hashtable_base& __x)
       {
        __hash_code_base::_M_swap(__x);
-       std::swap(_EqualEBO::_M_get(), __x._EqualEBO::_M_get());
+       using std::swap;
+       swap(_EqualEBO::_M_get(), __x._EqualEBO::_M_get());
       }
 
       const _Equal&
index 978093fc58779a46c56ee204e95fa24f06ff46ca..99575f6fe149208c6c769003a66a3232f326d2ce 100644 (file)
@@ -2089,7 +2089,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
        }
       // No need to swap header's color as it does not change.
-      std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
+
+      using std::swap;
+      swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
 
       _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
                                __t._M_get_Node_allocator());
diff --git a/libstdc++-v3/testsuite/23_containers/set/modifiers/swap/adl.cc b/libstdc++-v3/testsuite/23_containers/set/modifiers/swap/adl.cc
new file mode 100644 (file)
index 0000000..2b7975a
--- /dev/null
@@ -0,0 +1,54 @@
+// { dg-do run { target c++11 } }
+
+// Bug 117921 - containers do not use ADL swap for Compare, Pred or Hash types
+
+#include <set>
+#include <testsuite_hooks.h>
+
+namespace adl
+{
+  struct Less : std::less<int>
+  {
+    static bool swapped;
+    friend void swap(Less&, Less&) { swapped = true; }
+  };
+  bool Less::swapped = false;
+
+  struct Allocator_base
+  {
+    static bool swapped;
+  };
+  bool Allocator_base::swapped = false;
+
+  using std::size_t;
+
+  template<typename T>
+    struct Allocator : Allocator_base
+    {
+      using value_type = T;
+
+      Allocator() { }
+      template<typename U> Allocator(const Allocator<U>&) { }
+
+      T* allocate(size_t n) { return std::allocator<T>().allocate(n); }
+      void deallocate(T* p, size_t n) { std::allocator<T>().deallocate(p, n); }
+
+      using propagate_on_container_swap = std::true_type;
+
+      friend void swap(Allocator&, Allocator&) { swapped = true; }
+    };
+}
+
+void
+test_swap()
+{
+  std::set<int, adl::Less, adl::Allocator<int>> s1, s2;
+  s1.swap(s2);
+  VERIFY( adl::Less::swapped );
+  VERIFY( adl::Allocator_base::swapped );
+}
+
+int main()
+{
+  test_swap();
+}
diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/modifiers/swap-2.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/modifiers/swap-2.cc
new file mode 100644 (file)
index 0000000..a0fb1a6
--- /dev/null
@@ -0,0 +1,62 @@
+// { dg-do run { target c++11 } }
+
+// Bug 117921 - containers do not use ADL swap for Compare, Pred or Hash types
+
+#include <unordered_set>
+#include <testsuite_hooks.h>
+
+namespace adl
+{
+  struct Hash : std::hash<int>
+  {
+    static bool swapped;
+    friend void swap(Hash&, Hash&) { swapped = true; }
+  };
+  bool Hash::swapped = false;
+
+  struct Eq : std::equal_to<int>
+  {
+    static bool swapped;
+    friend void swap(Eq&, Eq&) { swapped = true; }
+  };
+  bool Eq::swapped = false;
+
+  struct Allocator_base
+  {
+    static bool swapped;
+  };
+  bool Allocator_base::swapped = false;
+
+  using std::size_t;
+
+  template<typename T>
+    struct Allocator : Allocator_base
+    {
+      using value_type = T;
+
+      Allocator() { }
+      template<typename U> Allocator(const Allocator<U>&) { }
+
+      T* allocate(size_t n) { return std::allocator<T>().allocate(n); }
+      void deallocate(T* p, size_t n) { std::allocator<T>().deallocate(p, n); }
+
+      using propagate_on_container_swap = std::true_type;
+
+      friend void swap(Allocator&, Allocator&) { swapped = true; }
+    };
+}
+
+void
+test_swap()
+{
+  std::unordered_set<int, adl::Eq, adl::Hash, adl::Allocator<int>> s1, s2;
+  s1.swap(s2);
+  VERIFY( adl::Hash::swapped );
+  VERIFY( adl::Eq::swapped );
+  VERIFY( adl::Allocator_base::swapped );
+}
+
+int main()
+{
+  test_swap();
+}