PR modula2/115112 Incorrect line debugging information occurs during INC builtin
[gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_map / modifiers / 78595.cc
blob2f55ec67053d2093c300aad2ae6c1ef47986895b
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.
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.
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/>.
18 // { dg-do run { target c++11 } }
20 #include <unordered_map>
21 #include <testsuite_hooks.h>
23 void
24 test01()
26 struct X {
27 mutable int conversions = 0;
29 operator std::pair<const int, int>() const {
30 if (++conversions > 1)
31 throw 1;
32 return {};
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 );
43 void
44 test02()
46 struct Y {
47 int conversions = 0;
49 operator std::pair<const int, int>() && {
50 if (++conversions > 1)
51 throw 1;
52 return {};
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 );
63 struct Key {
64 int key;
65 bool operator==(const Key& r) const { return key == r.key; }
68 namespace std {
69 template<> struct hash<Key> {
70 size_t operator()(const Key& k) const { return std::hash<int>()(k.key); }
74 struct Z {
75 operator std::pair<const Key, int>() const { return { { z }, 0 }; }
76 int z;
79 template<typename T>
80 struct Alloc
82 Alloc() = default;
84 template<typename U>
85 Alloc(const Alloc<U>&) { }
87 using value_type = T;
89 T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
91 void deallocate(T* p, std::size_t n) { std::allocator<T>().deallocate(p, n); }
93 template<typename U>
94 void construct(U* p, const Z& z) { ::new (p) U{ { z.z+1 }, 0}; }
96 template<typename U>
97 bool operator==(const Alloc<U>&) { return true; }
99 template<typename U>
100 bool operator!=(const Alloc<U>&) { return false; }
103 void
104 test03()
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 );
117 main()
119 test01();
120 test02();
121 test03();