Debian Codegraph @

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