Debian Codegraph @

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

raw
1 // { dg-do compile { target c++17 } }
2
3 #include <unordered_map>
4 #include <testsuite_allocator.h>
5
6 using __gnu_test::SimpleAllocator;
7
8 static_assert(std::is_same_v<
9 decltype(std::unordered_multimap{std::pair{1, 2.0},
10 {2, 3.0}, {3, 4.0}}),
11 std::unordered_multimap<int, double>>);
12
13 static_assert(std::is_same_v<
14 decltype(std::unordered_multimap{{std::pair{1, 2.0},
15 {2, 3.0}, {3, 4.0}}}),
16 std::unordered_multimap<int, double>>);
17
18 static_assert(std::is_same_v<
19 decltype(std::unordered_multimap{{std::pair{1, 2.0},
20 {2, 3.0}, {3, 4.0}},
21 {}, std::hash<int>{}, std::equal_to<int>{}}),
22 std::unordered_multimap<int, double>>);
23
24 static_assert(std::is_same_v<
25 decltype(std::unordered_multimap{{std::pair{1, 2.0},
26 {2, 3.0}, {3, 4.0}},
27 {}, std::hash<int>{}, std::allocator<std::pair<const int, double>>{}}),
28 std::unordered_multimap<int, double>>);
29
30 static_assert(std::is_same_v<
31 decltype(std::unordered_multimap{{std::pair{1, 2.0},
32 {2, 3.0}, {3, 4.0}},
33 {}}),
34 std::unordered_multimap<int, double>>);
35
36 static_assert(std::is_same_v<
37 decltype(std::unordered_multimap{
38 {std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
39 1, std::hash<int>{}}),
40 std::unordered_multimap<int, double>>);
41
42 static_assert(std::is_same_v<
43 decltype(std::unordered_multimap{
44 {std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
45 1, std::hash<int>{},
46 SimpleAllocator<std::pair<const int, double>>{}}),
47 std::unordered_multimap<int, double, std::hash<int>,
48 std::equal_to<int>,
49 SimpleAllocator<std::pair<const int, double>>>>);
50
51 static_assert(std::is_same_v<
52 decltype(std::unordered_multimap{
53 {std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
54 {}, {}, {},
55 SimpleAllocator<std::pair<const int, double>>{}}),
56 std::unordered_multimap<int, double, std::hash<int>,
57 std::equal_to<int>,
58 SimpleAllocator<std::pair<const int, double>>>>);
59
60
61 void f()
62 {
63 std::unordered_multimap<int, double> x;
64
65 static_assert(std::is_same_v<
66 decltype(std::unordered_multimap(x.begin(), x.end())),
67 std::unordered_multimap<int, double>>);
68
69 static_assert(std::is_same_v<
70 decltype(std::unordered_multimap{x.begin(), x.end(),
71 {}, std::hash<int>{}, {},
72 std::allocator<std::pair<const int, double>>{}}),
73 std::unordered_multimap<int, double>>);
74
75 static_assert(std::is_same_v<
76 decltype(std::unordered_multimap{x.begin(), x.end(),
77 {}, std::hash<int>{}, std::equal_to<int>{}}),
78 std::unordered_multimap<int, double>>);
79
80 static_assert(std::is_same_v<
81 decltype(std::unordered_multimap{x.begin(), x.end(),
82 {}, std::hash<int>{}, std::allocator<std::pair<const int, double>>{}}),
83 std::unordered_multimap<int, double>>);
84
85 static_assert(std::is_same_v<
86 decltype(std::unordered_multimap(x.begin(), x.end(),
87 {})),
88 std::unordered_multimap<int, double>>);
89
90 static_assert(std::is_same_v<
91 decltype(std::unordered_multimap(x.begin(), x.end(), 1)),
92 std::unordered_multimap<int, double>>);
93
94 static_assert(std::is_same_v<
95 decltype(std::unordered_multimap{x.begin(), x.end(),
96 {},
97 std::allocator<std::pair<const int, double>>{}}),
98 std::unordered_multimap<int, double>>);
99
100 static_assert(std::is_same_v<
101 decltype(std::unordered_multimap{x.begin(), x.end(),
102 {},
103 SimpleAllocator<std::pair<const int, double>>{}}),
104 std::unordered_multimap<int, double, std::hash<int>,
105 std::equal_to<int>,
106 SimpleAllocator<std::pair<const int, double>>>>);
107
108 static_assert(std::is_same_v<
109 decltype(std::unordered_multimap{x.begin(), x.end(),
110 1, std::hash<int>{},
111 std::allocator<std::pair<const int, double>>{}}),
112 std::unordered_multimap<int, double>>);
113
114 static_assert(std::is_same_v<
115 decltype(std::unordered_multimap{x.begin(), x.end(),
116 1, std::hash<int>{},
117 SimpleAllocator<std::pair<const int, double>>{}}),
118 std::unordered_multimap<int, double, std::hash<int>,
119 std::equal_to<int>,
120 SimpleAllocator<std::pair<const int, double>>>>);
121
122 static_assert(std::is_same_v<
123 decltype(std::unordered_multimap{x.begin(), x.end(),
124 {}, {}, {},
125 std::allocator<std::pair<const int, double>>{}}),
126 std::unordered_multimap<int, double>>);
127
128 static_assert(std::is_same_v<
129 decltype(std::unordered_multimap{x.begin(), x.end(),
130 {}, {}, {},
131 SimpleAllocator<std::pair<const int, double>>{}}),
132 std::unordered_multimap<int, double, std::hash<int>,
133 std::equal_to<int>,
134 SimpleAllocator<std::pair<const int, double>>>>);
135 }
136
137 template<typename T, typename U> struct require_same;
138 template<typename T> struct require_same<T, T> { using type = void; };
139
140 template<typename T, typename U>
141 typename require_same<T, U>::type
142 check_type(U&) { }
143
144 struct Pool;
145
146 template<typename T>
147 struct Alloc : __gnu_test::SimpleAllocator<T>
148 {
149 Alloc(Pool*) { }
150
151 template<typename U>
152 Alloc(const Alloc<U>&) { }
153 };
154
155 void
156 test_p1518r2()
157 {
158 // P1518R2 - Stop overconstraining allocators in container deduction guides.
159 // This is a C++23 feature but we support it for C++17 too.
160
161 using PairAlloc = Alloc<std::pair<const unsigned, void*>>;
162 using Hash = std::hash<unsigned long>;
163 using Eq = std::equal_to<>;
164 using UMMap = std::unordered_multimap<unsigned, void*, Hash, Eq, PairAlloc>;
165 Pool* p = nullptr;
166 UMMap m(p);
167
168 std::unordered_multimap s1(m, p);
169 check_type<UMMap>(s1);
170
171 std::unordered_multimap s2(std::move(m), p);
172 check_type<UMMap>(s2);
173 }