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)
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(); }
_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&
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());
--- /dev/null
+// { 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();
+}
--- /dev/null
+// { 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();
+}