| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Bug 117921 - containers do not use ADL swap for Compare, Pred or Hash types |
| 4 | |
| 5 | #include <unordered_set> |
| 6 | #include <testsuite_hooks.h> |
| 7 | |
| 8 | namespace adl |
| 9 | { |
| 10 | struct Hash : std::hash<int> |
| 11 | { |
| 12 | static bool swapped; |
| 13 | friend void swap(Hash&, Hash&) { swapped = true; } |
| 14 | }; |
| 15 | bool Hash::swapped = false; |
| 16 | |
| 17 | struct Eq : std::equal_to<int> |
| 18 | { |
| 19 | static bool swapped; |
| 20 | friend void swap(Eq&, Eq&) { swapped = true; } |
| 21 | }; |
| 22 | bool Eq::swapped = false; |
| 23 | |
| 24 | struct Allocator_base |
| 25 | { |
| 26 | static bool swapped; |
| 27 | }; |
| 28 | bool Allocator_base::swapped = false; |
| 29 | |
| 30 | using std::size_t; |
| 31 | |
| 32 | template<typename T> |
| 33 | struct Allocator : Allocator_base |
| 34 | { |
| 35 | using value_type = T; |
| 36 | |
| 37 | Allocator() { } |
| 38 | template<typename U> Allocator(const Allocator<U>&) { } |
| 39 | |
| 40 | T* allocate(size_t n) { return std::allocator<T>().allocate(n); } |
| 41 | void deallocate(T* p, size_t n) { std::allocator<T>().deallocate(p, n); } |
| 42 | |
| 43 | using propagate_on_container_swap = std::true_type; |
| 44 | |
| 45 | friend void swap(Allocator&, Allocator&) { swapped = true; } |
| 46 | friend bool operator==(Allocator, Allocator) { return true; } |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | test_swap() |
| 52 | { |
| 53 | std::unordered_set<int, adl::Eq, adl::Hash, adl::Allocator<int>> s1, s2; |
| 54 | s1.swap(s2); |
| 55 | VERIFY( adl::Hash::swapped ); |
| 56 | VERIFY( adl::Eq::swapped ); |
| 57 | VERIFY( adl::Allocator_base::swapped ); |
| 58 | } |
| 59 | |
| 60 | int main() |
| 61 | { |
| 62 | test_swap(); |
| 63 | } |