Debian Codegraph @

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

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