1 
2 // Copyright 2017-2018 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include <boost/unordered_map.hpp>
7 #include <iostream>
8 #include <vector>
9 
10 #if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
11 struct hash_equals
12 {
operator ()hash_equals13   template <typename T> bool operator()(T const& x) const
14   {
15     boost::hash<T> hf;
16     return hf(x);
17   }
18 
operator ()hash_equals19   template <typename T> bool operator()(T const& x, T const& y) const
20   {
21     std::equal_to<T> eq;
22     return eq(x, y);
23   }
24 };
25 
26 template <typename T> struct test_allocator
27 {
28   typedef T value_type;
29   test_allocator() = default;
test_allocatortest_allocator30   template <typename T2> test_allocator(test_allocator<T2> const&) {}
allocatetest_allocator31   T* allocate(std::size_t n) const { return (T*)malloc(sizeof(T) * n); }
deallocatetest_allocator32   void deallocate(T* ptr, std::size_t) const { free(ptr); }
operator ==test_allocator33   bool operator==(test_allocator const&) { return true; }
operator !=test_allocator34   bool operator!=(test_allocator const&) { return false; }
35 };
36 #endif
37 
main()38 int main()
39 {
40   std::cout << "BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES: "
41             << BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES << std::endl;
42 
43 #if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
44   std::vector<std::pair<int, int> > x;
45   x.push_back(std::make_pair(1, 3));
46   x.push_back(std::make_pair(5, 10));
47   test_allocator<std::pair<const int, int> > pair_allocator;
48   hash_equals f;
49 
50   // unordered_map
51 
52   /*
53    template<class InputIterator,
54            class Hash = hash<iter_key_t<InputIterator>>,
55            class Pred = equal_to<iter_key_t<InputIterator>>,
56            class Allocator = allocator<iter_to_alloc_t<InputIterator>>>
57     unordered_map(InputIterator, InputIterator, typename see below::size_type =
58    see below,
59                   Hash = Hash(), Pred = Pred(), Allocator = Allocator())
60       -> unordered_map<iter_key_t<InputIterator>, iter_val_t<InputIterator>,
61    Hash, Pred,
62                        Allocator>;
63   */
64 
65   {
66     boost::unordered_map m(x.begin(), x.end());
67     static_assert(
68       std::is_same<decltype(m), boost::unordered_map<int, int> >::value);
69   }
70 
71   /* Ambiguous:
72   {
73     boost::unordered_map m(x.begin(), x.end(), 0, std::hash<int>());
74     static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
75   std::hash<int>>>::value);
76   }
77 
78   {
79     boost::unordered_map m(x.begin(), x.end(), 0, std::hash<int>(),
80   std::equal_to<int>());
81     static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
82   std::hash<int>, std::equal_to<int>>>::value);
83   }
84   */
85 
86   {
87     boost::unordered_map m(x.begin(), x.end(), 0, std::hash<int>(),
88       std::equal_to<int>(), pair_allocator);
89     static_assert(std::is_same<decltype(m),
90       boost::unordered_map<int, int, std::hash<int>, std::equal_to<int>,
91         test_allocator<std::pair<const int, int> > > >::value);
92   }
93 
94   /*
95   template<class Key, class T, class Hash = hash<Key>,
96            class Pred = equal_to<Key>, class Allocator = allocator<pair<const
97   Key, T>>>
98     unordered_map(initializer_list<pair<const Key, T>>,
99                   typename see below::size_type = see below, Hash = Hash(),
100                   Pred = Pred(), Allocator = Allocator())
101       -> unordered_map<Key, T, Hash, Pred, Allocator>;
102   */
103 
104   {
105     boost::unordered_map m({std::pair<int const, int>(1, 2)});
106     static_assert(
107       std::is_same<decltype(m), boost::unordered_map<int, int> >::value);
108   }
109 
110   /* Ambiguous
111   {
112     boost::unordered_map m({std::pair<int const, int>(1,2)}, 0,
113   std::hash<int>());
114     static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
115   std::hash<int>>>::value);
116   }
117 
118   {
119     boost::unordered_map m({std::pair<int const, int>(1,2)}, 0,
120   std::hash<int>(), std::equal_to<int>());
121     static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
122   std::hash<int>, std::equal_to<int>>>::value);
123   }
124   */
125 
126   {
127     boost::unordered_map m(
128       {std::pair<int const, int>(1, 2)}, 0, f, f, pair_allocator);
129     static_assert(std::is_same<decltype(m),
130       boost::unordered_map<int, int, hash_equals, hash_equals,
131         test_allocator<std::pair<const int, int> > > >::value);
132   }
133 
134   /*
135   template<class InputIterator, class Allocator>
136     unordered_map(InputIterator, InputIterator, typename see below::size_type,
137   Allocator)
138       -> unordered_map<iter_key_t<InputIterator>, iter_val_t<InputIterator>,
139                        hash<iter_key_t<InputIterator>>,
140   equal_to<iter_key_t<InputIterator>>,
141                        Allocator>;
142   */
143 
144   /* Ambiguous
145   {
146     boost::unordered_map m(x.begin(), x.end(), 0u, pair_allocator);
147     static_assert(std::is_same<decltype(m), boost::unordered_map<int, int,
148   boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
149   int>>>>::value);
150   }
151   */
152 
153   /*
154     template<class InputIterator, class Allocator>
155     unordered_map(InputIterator, InputIterator, Allocator)
156       -> unordered_map<iter_key_t<InputIterator>, iter_val_t<InputIterator>,
157                        hash<iter_key_t<InputIterator>>,
158     equal_to<iter_key_t<InputIterator>>,
159                        Allocator>;
160   */
161 
162   /* No constructor:
163   {
164     boost::unordered_map m(x.begin(), x.end(), pair_allocator);
165     static_assert(std::is_same<decltype(m), boost::unordered_map<int, int,
166   boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
167   int>>>>::value);
168   }
169   */
170 
171   /*
172   template<class InputIterator, class Hash, class Allocator>
173     unordered_map(InputIterator, InputIterator, typename see below::size_type,
174   Hash, Allocator)
175       -> unordered_map<iter_key_t<InputIterator>, iter_val_t<InputIterator>,
176   Hash,
177                        equal_to<iter_key_t<InputIterator>>, Allocator>;
178   */
179 
180   /* Ambiguous
181   {
182     boost::unordered_map m(x.begin(), x.end(), 0u, f, pair_allocator);
183     static_assert(std::is_same<decltype(m), boost::unordered_map<int, int,
184   hash_equals, std::equal_to<int>, test_allocator<std::pair<const int,
185   int>>>>::value);
186   }
187   */
188 
189   /*
190     template<class Key, class T, typename Allocator>
191     unordered_map(initializer_list<pair<const Key, T>>, typename see
192     below::size_type,
193                   Allocator)
194       -> unordered_map<Key, T, hash<Key>, equal_to<Key>, Allocator>;
195   */
196 
197   /* Ambiguous
198   {
199     boost::unordered_map m({std::pair<int const, int>(1,2)}, 0, pair_allocator);
200     static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
201   boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
202   int>>>>::value);
203   }
204   */
205 
206   /*
207   template<class Key, class T, typename Allocator>
208     unordered_map(initializer_list<pair<const Key, T>>, Allocator)
209       -> unordered_map<Key, T, hash<Key>, equal_to<Key>, Allocator>;
210   */
211 
212   {
213     boost::unordered_map m({std::pair<int const, int>(1, 2)}, pair_allocator);
214     static_assert(std::is_same<decltype(m),
215       boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>,
216         test_allocator<std::pair<const int, int> > > >::value);
217   }
218 
219   /*
220   template<class Key, class T, class Hash, class Allocator>
221     unordered_map(initializer_list<pair<const Key, T>>, typename see
222   below::size_type, Hash,
223                   Allocator)
224       -> unordered_map<Key, T, Hash, equal_to<Key>, Allocator>;
225   */
226 
227   /* Ambiguous
228   {
229     boost::unordered_map m({std::pair<int const, int>(1,2)}, 0, f,
230   pair_allocator);
231     static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
232   boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
233   int>>>>::value);
234   }
235   */
236 
237   // unordered_multimap
238 
239   {
240     boost::unordered_multimap m(x.begin(), x.end());
241     static_assert(
242       std::is_same<decltype(m), boost::unordered_multimap<int, int> >::value);
243   }
244 
245   /* Ambiguous:
246   {
247     boost::unordered_multimap m(x.begin(), x.end(), 0, std::hash<int>());
248     static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
249   std::hash<int>>>::value);
250   }
251 
252   {
253     boost::unordered_multimap m(x.begin(), x.end(), 0, std::hash<int>(),
254   std::equal_to<int>());
255     static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
256   std::hash<int>, std::equal_to<int>>>::value);
257   }
258   */
259 
260   {
261     boost::unordered_multimap m(x.begin(), x.end(), 0, std::hash<int>(),
262       std::equal_to<int>(), pair_allocator);
263     static_assert(std::is_same<decltype(m),
264       boost::unordered_multimap<int, int, std::hash<int>, std::equal_to<int>,
265         test_allocator<std::pair<const int, int> > > >::value);
266   }
267 
268   {
269     boost::unordered_multimap m({std::pair<int const, int>(1, 2)});
270     static_assert(
271       std::is_same<decltype(m), boost::unordered_multimap<int, int> >::value);
272   }
273 
274   /* Ambiguous
275   {
276     boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0,
277   std::hash<int>());
278     static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
279   std::hash<int>>>::value);
280   }
281 
282   {
283     boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0,
284   std::hash<int>(), std::equal_to<int>());
285     static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
286   std::hash<int>, std::equal_to<int>>>::value);
287   }
288   */
289 
290   {
291     boost::unordered_multimap m(
292       {std::pair<int const, int>(1, 2)}, 0, f, f, pair_allocator);
293     static_assert(std::is_same<decltype(m),
294       boost::unordered_multimap<int, int, hash_equals, hash_equals,
295         test_allocator<std::pair<const int, int> > > >::value);
296   }
297 
298   /* Ambiguous
299   {
300     boost::unordered_multimap m(x.begin(), x.end(), 0u, pair_allocator);
301     static_assert(std::is_same<decltype(m), boost::unordered_multimap<int, int,
302   boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
303   int>>>>::value);
304   }
305   */
306 
307   /* No constructor:
308   {
309     boost::unordered_multimap m(x.begin(), x.end(), pair_allocator);
310     static_assert(std::is_same<decltype(m), boost::unordered_multimap<int, int,
311   boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
312   int>>>>::value);
313   }
314   */
315 
316   /* Ambiguous
317   {
318     boost::unordered_multimap m(x.begin(), x.end(), 0u, f, pair_allocator);
319     static_assert(std::is_same<decltype(m), boost::unordered_multimap<int, int,
320   hash_equals, std::equal_to<int>, test_allocator<std::pair<const int,
321   int>>>>::value);
322   }
323 
324   {
325     boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0,
326   pair_allocator);
327     static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
328   boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
329   int>>>>::value);
330   }
331   */
332 
333   {
334     boost::unordered_multimap m(
335       {std::pair<int const, int>(1, 2)}, pair_allocator);
336     static_assert(std::is_same<decltype(m),
337       boost::unordered_multimap<int, int, boost::hash<int>, std::equal_to<int>,
338         test_allocator<std::pair<const int, int> > > >::value);
339   }
340 
341 /* Ambiguous
342 {
343   boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0, f,
344 pair_allocator);
345   static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
346 boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
347 int>>>>::value);
348 }
349 */
350 
351 #endif
352 }
353