| 1 | // Copyright (C) 2018-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++11 } } |
| 19 | |
| 20 | #include <unordered_map> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | void |
| 24 | test01() |
| 25 | { |
| 26 | struct X { |
| 27 | mutable int conversions = 0; |
| 28 | |
| 29 | operator std::pair<const int, int>() const { |
| 30 | if (++conversions > 1) |
| 31 | throw 1; |
| 32 | return {}; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | std::unordered_map<int, int> m; |
| 37 | m.insert(X()); |
| 38 | VERIFY( m.size() == 1 ); |
| 39 | m.insert(m.begin(), X()); |
| 40 | VERIFY( m.size() == 1 ); |
| 41 | |
| 42 | } |
| 43 | void |
| 44 | test02() |
| 45 | { |
| 46 | struct Y { |
| 47 | int conversions = 0; |
| 48 | |
| 49 | operator std::pair<const int, int>() && { |
| 50 | if (++conversions > 1) |
| 51 | throw 1; |
| 52 | return {}; |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | std::unordered_map<int, int> m; |
| 57 | m.insert(Y()); |
| 58 | VERIFY( m.size() == 1 ); |
| 59 | m.insert(m.begin(), Y()); |
| 60 | VERIFY( m.size() == 1 ); |
| 61 | } |
| 62 | |
| 63 | struct Key { |
| 64 | int key; |
| 65 | bool operator==(const Key& r) const { return key == r.key; } |
| 66 | }; |
| 67 | |
| 68 | namespace std { |
| 69 | template<> struct hash<Key> { |
| 70 | size_t operator()(const Key& k) const { return std::hash<int>()(k.key); } |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | struct Z { |
| 75 | operator std::pair<const Key, int>() const { return { { z }, 0 }; } |
| 76 | int z; |
| 77 | }; |
| 78 | |
| 79 | template<typename T> |
| 80 | struct Alloc |
| 81 | { |
| 82 | Alloc() = default; |
| 83 | |
| 84 | template<typename U> |
| 85 | Alloc(const Alloc<U>&) { } |
| 86 | |
| 87 | using value_type = T; |
| 88 | |
| 89 | T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); } |
| 90 | |
| 91 | void deallocate(T* p, std::size_t n) { std::allocator<T>().deallocate(p, n); } |
| 92 | |
| 93 | template<typename U> |
| 94 | void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; } |
| 95 | |
| 96 | template<typename U> |
| 97 | bool operator==(const Alloc<U>&) { return true; } |
| 98 | |
| 99 | template<typename U> |
| 100 | bool operator!=(const Alloc<U>&) { return false; } |
| 101 | }; |
| 102 | |
| 103 | void |
| 104 | test03() |
| 105 | { |
| 106 | std::unordered_map<Key, int, std::hash<Key>, std::equal_to<Key>, |
| 107 | Alloc<std::pair<const Key, int>>> m; |
| 108 | m.insert(Z{}); |
| 109 | m.insert(Z{}); |
| 110 | VERIFY( m.size() == 1 ); |
| 111 | m.insert(Z{}); |
| 112 | m.insert(Z{1}); |
| 113 | VERIFY( m.size() == 2 ); |
| 114 | } |
| 115 | |
| 116 | int |
| 117 | main() |
| 118 | { |
| 119 | test01(); |
| 120 | test02(); |
| 121 | test03(); |
| 122 | } |