1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <unordered_map>
11
12 // void swap(unordered_multimap& c)
13 // noexcept(
14 // (!allocator_type::propagate_on_container_swap::value ||
15 // __is_nothrow_swappable<allocator_type>::value) &&
16 // __is_nothrow_swappable<hasher>::value &&
17 // __is_nothrow_swappable<key_equal>::value);
18 //
19 // In C++17, the standard says that swap shall have:
20 // noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
21 // __is_nothrow_swappable<hasher>::value &&
22 // __is_nothrow_swappable<key_equal>::value);
23
24 // This tests a conforming extension
25
26 #include <unordered_map>
27 #include <cassert>
28
29 #include "MoveOnly.h"
30 #include "test_allocator.h"
31
32 template <class T>
33 struct some_comp
34 {
35 typedef T value_type;
36
some_compsome_comp37 some_comp() {}
some_compsome_comp38 some_comp(const some_comp&) {}
39 };
40
41 template <class T>
42 struct some_comp2
43 {
44 typedef T value_type;
45
some_comp2some_comp246 some_comp2() {}
some_comp2some_comp247 some_comp2(const some_comp2&) {}
deallocatesome_comp248 void deallocate(void*, unsigned) {}
49 typedef std::true_type propagate_on_container_swap;
50 };
51
52 #if TEST_STD_VER >= 14
53 template <typename T>
swap(some_comp2<T> &,some_comp2<T> &)54 void swap(some_comp2<T>&, some_comp2<T>&) noexcept {}
55 #endif
56
57 template <class T>
58 struct some_hash
59 {
60 typedef T value_type;
some_hashsome_hash61 some_hash() {}
62 some_hash(const some_hash&);
63 };
64
65 template <class T>
66 struct some_hash2
67 {
68 typedef T value_type;
some_hash2some_hash269 some_hash2() {}
70 some_hash2(const some_hash2&);
71 };
72
73 #if TEST_STD_VER >= 14
74 template <typename T>
swap(some_hash2<T> &,some_hash2<T> &)75 void swap(some_hash2<T>&, some_hash2<T>&) noexcept {}
76 #endif
77
78 template <class T>
79 struct some_alloc
80 {
81 typedef T value_type;
82
some_allocsome_alloc83 some_alloc() {}
84 some_alloc(const some_alloc&);
deallocatesome_alloc85 void deallocate(void*, unsigned) {}
86
87 typedef std::true_type propagate_on_container_swap;
88 };
89
90 template <class T>
91 struct some_alloc2
92 {
93 typedef T value_type;
94
some_alloc2some_alloc295 some_alloc2() {}
96 some_alloc2(const some_alloc2&);
deallocatesome_alloc297 void deallocate(void*, unsigned) {}
98
99 typedef std::false_type propagate_on_container_swap;
100 typedef std::true_type is_always_equal;
101 };
102
103 template <class T>
104 struct some_alloc3
105 {
106 typedef T value_type;
107
some_alloc3some_alloc3108 some_alloc3() {}
109 some_alloc3(const some_alloc3&);
deallocatesome_alloc3110 void deallocate(void*, unsigned) {}
111
112 typedef std::false_type propagate_on_container_swap;
113 typedef std::false_type is_always_equal;
114 };
115
main()116 int main()
117 {
118 #if __has_feature(cxx_noexcept)
119 typedef std::pair<const MoveOnly, MoveOnly> MapType;
120 {
121 typedef std::unordered_multimap<MoveOnly, MoveOnly> C;
122 C c1, c2;
123 static_assert(noexcept(swap(c1, c2)), "");
124 }
125 {
126 typedef std::unordered_multimap<MoveOnly, MoveOnly, std::hash<MoveOnly>,
127 std::equal_to<MoveOnly>, test_allocator<MapType>> C;
128 C c1, c2;
129 static_assert(noexcept(swap(c1, c2)), "");
130 }
131 {
132 typedef std::unordered_multimap<MoveOnly, MoveOnly, std::hash<MoveOnly>,
133 std::equal_to<MoveOnly>, other_allocator<MapType>> C;
134 C c1, c2;
135 static_assert(noexcept(swap(c1, c2)), "");
136 }
137 {
138 typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash<MoveOnly>> C;
139 C c1, c2;
140 static_assert(!noexcept(swap(c1, c2)), "");
141 }
142 {
143 typedef std::unordered_multimap<MoveOnly, MoveOnly, std::hash<MoveOnly>,
144 some_comp<MoveOnly>> C;
145 C c1, c2;
146 static_assert(!noexcept(swap(c1, c2)), "");
147 }
148
149 #if TEST_STD_VER >= 14
150 { // POCS allocator, throwable swap for hash, throwable swap for comp
151 typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp <MoveOnly>, some_alloc <MapType>> C;
152 C c1, c2;
153 static_assert(!noexcept(swap(c1, c2)), "");
154 }
155 { // always equal allocator, throwable swap for hash, throwable swap for comp
156 typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp <MoveOnly>, some_alloc2<MapType>> C;
157 C c1, c2;
158 static_assert(!noexcept(swap(c1, c2)), "");
159 }
160 { // POCS allocator, throwable swap for hash, nothrow swap for comp
161 typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp2<MoveOnly>, some_alloc <MapType>> C;
162 C c1, c2;
163 static_assert(!noexcept(swap(c1, c2)), "");
164 }
165 { // always equal allocator, throwable swap for hash, nothrow swap for comp
166 typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash<MoveOnly>, some_comp2<MoveOnly>, some_alloc2<MapType>> C;
167 C c1, c2;
168 static_assert(!noexcept(swap(c1, c2)), "");
169 }
170 { // POCS allocator, nothrow swap for hash, throwable swap for comp
171 typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp <MoveOnly>, some_alloc <MapType>> C;
172 C c1, c2;
173 static_assert(!noexcept(swap(c1, c2)), "");
174 }
175 { // always equal allocator, nothrow swap for hash, throwable swap for comp
176 typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp <MoveOnly>, some_alloc2<MapType>> C;
177 C c1, c2;
178 static_assert(!noexcept(swap(c1, c2)), "");
179 }
180 { // POCS allocator, nothrow swap for hash, nothrow swap for comp
181 typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc <MapType>> C;
182 C c1, c2;
183 static_assert( noexcept(swap(c1, c2)), "");
184 }
185 { // always equal allocator, nothrow swap for hash, nothrow swap for comp
186 typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc2<MapType>> C;
187 C c1, c2;
188 static_assert( noexcept(swap(c1, c2)), "");
189 }
190 { // NOT always equal allocator, nothrow swap for hash, nothrow swap for comp
191 typedef std::unordered_multimap<MoveOnly, MoveOnly, some_hash2<MoveOnly>, some_comp2<MoveOnly>, some_alloc3<MapType>> C;
192 C c1, c2;
193 static_assert( noexcept(swap(c1, c2)), "");
194 }
195 #endif
196
197 #endif
198 }
199