Debian Codegraph @

libstdc++-v3/testsuite/23_containers/map/cons/deduction.cc

raw
1 // { dg-do compile { target c++17 } }
2
3 #include <map>
4 #include <testsuite_allocator.h>
5 #include <testsuite_iterators.h>
6
7 using __gnu_test::SimpleAllocator;
8 using value_type = std::map<int, double>::value_type;
9
10 static_assert(std::is_same_v<
11 decltype(std::map{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}}),
12 std::map<int, double>>);
13
14 static_assert(std::is_same_v<
15 decltype(std::map{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}}),
16 std::map<int, double>>);
17
18 static_assert(std::is_same_v<
19 decltype(std::map{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}}}),
20 std::map<int, double>>);
21
22 static_assert(std::is_same_v<
23 decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}}}),
24 std::map<int, double>>);
25
26 static_assert(std::is_same_v<
27 decltype(std::map{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}},
28 std::less<int>{}, {}}),
29 std::map<int, double>>);
30
31 static_assert(std::is_same_v<
32 decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
33 std::less<int>{}, {}}),
34 std::map<int, double>>);
35
36 /* This is not deducible, {} could be deduced as _Compare or _Allocator.
37 static_assert(std::is_same_v<
38 decltype(std::map{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}}, {}}),
39 std::map<int, double>>);
40 */
41
42 static_assert(std::is_same_v<
43 decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}}, std::less<int>{}}),
44 std::map<int, double>>);
45
46 /* This is not deducible, ambiguous candidates:
47 * map(initializer_list<value_type>, const Compare&, const _Allocator& = {})
48 * map(initializer_list<value_type>, const _Allocator&)
49 * map(initializer_list<pair<Key, T>>, const _Allocator&) -> map
50 static_assert(std::is_same_v<
51 decltype(std::map{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}},
52 SimpleAllocator<value_type>{}}),
53 std::map<int, double, std::less<int>,
54 SimpleAllocator<value_type>>>);
55 */
56
57 static_assert(std::is_same_v<
58 decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
59 SimpleAllocator<value_type>{}}),
60 std::map<int, double, std::less<int>,
61 SimpleAllocator<value_type>>>);
62
63 static_assert(std::is_same_v<
64 decltype(std::map{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}},
65 {}, SimpleAllocator<value_type>{}}),
66 std::map<int, double, std::less<int>,
67 SimpleAllocator<value_type>>>);
68
69 static_assert(std::is_same_v<
70 decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
71 {}, SimpleAllocator<value_type>{}}),
72 std::map<int, double, std::less<int>,
73 SimpleAllocator<value_type>>>);
74
75 void f()
76 {
77 std::map<int, double> x;
78 static_assert(std::is_same_v<
79 decltype(std::map(x.begin(), x.end())),
80 std::map<int, double>>);
81
82 static_assert(std::is_same_v<
83 decltype(std::map{x.begin(), x.end(),
84 std::less<int>{},
85 std::allocator<value_type>{}}),
86 std::map<int, double>>);
87
88 static_assert(std::is_same_v<
89 decltype(std::map{x.begin(), x.end(),
90 std::less<int>{}, {}}),
91 std::map<int, double>>);
92
93 static_assert(std::is_same_v<
94 decltype(std::map(x.begin(), x.end(),
95 std::less<int>{})),
96 std::map<int, double>>);
97
98 static_assert(std::is_same_v<
99 decltype(std::map{x.begin(), x.end(),
100 std::allocator<value_type>{}}),
101 std::map<int, double>>);
102
103 static_assert(std::is_same_v<
104 decltype(std::map{x.begin(), x.end(),
105 SimpleAllocator<value_type>{}}),
106 std::map<int, double, std::less<int>,
107 SimpleAllocator<value_type>>>);
108
109 static_assert(std::is_same_v<
110 decltype(std::map{x.begin(), x.end(),
111 {},
112 std::allocator<value_type>{}}),
113 std::map<int, double>>);
114
115 static_assert(std::is_same_v<
116 decltype(std::map{x.begin(), x.end(),
117 {},
118 SimpleAllocator<value_type>{}}),
119 std::map<int, double, std::less<int>,
120 SimpleAllocator<value_type>>>);
121 }
122
123 using __gnu_test::test_container;
124 using __gnu_test::input_iterator_wrapper;
125
126 void g()
127 {
128 value_type array[1];
129 test_container<value_type, input_iterator_wrapper> x(array);
130
131 static_assert(std::is_same_v<
132 decltype(std::map(x.begin(), x.end())),
133 std::map<int, double>>);
134
135 static_assert(std::is_same_v<
136 decltype(std::map{x.begin(), x.end(),
137 std::less<int>{},
138 std::allocator<value_type>{}}),
139 std::map<int, double>>);
140
141 static_assert(std::is_same_v<
142 decltype(std::map{x.begin(), x.end(),
143 std::less<int>{}, {}}),
144 std::map<int, double>>);
145
146 static_assert(std::is_same_v<
147 decltype(std::map(x.begin(), x.end(),
148 std::less<int>{})),
149 std::map<int, double>>);
150
151 static_assert(std::is_same_v<
152 decltype(std::map{x.begin(), x.end(),
153 std::allocator<value_type>{}}),
154 std::map<int, double>>);
155
156 static_assert(std::is_same_v<
157 decltype(std::map{x.begin(), x.end(),
158 SimpleAllocator<value_type>{}}),
159 std::map<int, double, std::less<int>,
160 SimpleAllocator<value_type>>>);
161
162 static_assert(std::is_same_v<
163 decltype(std::map{x.begin(), x.end(),
164 {},
165 std::allocator<value_type>{}}),
166 std::map<int, double>>);
167
168 static_assert(std::is_same_v<
169 decltype(std::map{x.begin(), x.end(),
170 {},
171 SimpleAllocator<value_type>{}}),
172 std::map<int, double, std::less<int>,
173 SimpleAllocator<value_type>>>);
174 }
175
176 void h()
177 {
178 std::pair<int, double> array[1];
179 test_container<std::pair<int, double>, input_iterator_wrapper> x(array);
180
181 static_assert(std::is_same_v<
182 decltype(std::map(x.begin(), x.end())),
183 std::map<int, double>>);
184
185 static_assert(std::is_same_v<
186 decltype(std::map{x.begin(), x.end(),
187 std::less<int>{},
188 std::allocator<value_type>{}}),
189 std::map<int, double>>);
190
191 static_assert(std::is_same_v<
192 decltype(std::map{x.begin(), x.end(),
193 std::less<int>{}, {}}),
194 std::map<int, double>>);
195
196 static_assert(std::is_same_v<
197 decltype(std::map(x.begin(), x.end(),
198 std::less<int>{})),
199 std::map<int, double>>);
200
201 static_assert(std::is_same_v<
202 decltype(std::map{x.begin(), x.end(),
203 std::allocator<value_type>{}}),
204 std::map<int, double>>);
205
206 static_assert(std::is_same_v<
207 decltype(std::map{x.begin(), x.end(),
208 SimpleAllocator<value_type>{}}),
209 std::map<int, double, std::less<int>,
210 SimpleAllocator<value_type>>>);
211
212 static_assert(std::is_same_v<
213 decltype(std::map{x.begin(), x.end(),
214 {},
215 std::allocator<value_type>{}}),
216 std::map<int, double>>);
217
218 static_assert(std::is_same_v<
219 decltype(std::map{x.begin(), x.end(),
220 {},
221 SimpleAllocator<value_type>{}}),
222 std::map<int, double, std::less<int>,
223 SimpleAllocator<value_type>>>);
224 }
225
226 template<typename T, typename U> struct require_same;
227 template<typename T> struct require_same<T, T> { using type = void; };
228
229 template<typename T, typename U>
230 typename require_same<T, U>::type
231 check_type(U&) { }
232
233 struct Pool;
234
235 template<typename T>
236 struct Alloc : __gnu_test::SimpleAllocator<T>
237 {
238 Alloc(Pool*) { }
239
240 template<typename U>
241 Alloc(const Alloc<U>&) { }
242 };
243
244 void
245 test_p1518r2()
246 {
247 // P1518R2 - Stop overconstraining allocators in container deduction guides.
248 // This is a C++23 feature but we support it for C++17 too.
249
250 using PairAlloc = Alloc<std::pair<const unsigned, void*>>;
251 using Map = std::map<unsigned, void*, std::greater<>, PairAlloc>;
252 Pool* p = nullptr;
253 Map m(p);
254
255 std::map s1(m, p);
256 check_type<Map>(s1);
257
258 std::map s2(std::move(m), p);
259 check_type<Map>(s2);
260 }