Debian Codegraph @

libstdc++-v3/testsuite/20_util/variable_templates_for_traits.cc

raw
1 // { dg-additional-options "-Wno-deprecated" { target c++2a } }
2 // { dg-do compile { target c++17 } }
3
4 // Copyright (C) 2014-2025 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 #include <type_traits>
22
23 using namespace std;
24
25 // These tests are rather simple, the front-end tests already test
26 // variable templates, and the library tests for the underlying
27 // traits are more elaborate. These are just simple sanity tests.
28
29 static_assert(is_void_v<void> && is_void<void>::value, "");
30 static_assert(!is_void_v<int> && !is_void<int>::value, "");
31
32 static_assert(is_null_pointer_v<nullptr_t>
33 && is_null_pointer<nullptr_t>::value, "");
34 static_assert(!is_null_pointer_v<void*>
35 && !is_null_pointer<void*>::value, "");
36
37 static_assert(is_integral_v<int> && is_integral<int>::value, "");
38 static_assert(!is_integral_v<int*> && !is_integral<int*>::value, "");
39
40 static_assert(is_floating_point_v<float>
41 && is_floating_point<float>::value, "");
42 static_assert(!is_floating_point_v<int>
43 && !is_floating_point<int>::value, "");
44
45 static_assert(is_array_v<char[42]> && is_array<char[42]>::value, "");
46 static_assert(!is_array_v<char*> && !is_array<char*>::value, "");
47
48 static_assert(is_pointer_v<int*> && is_pointer<int*>::value, "");
49 static_assert(!is_pointer_v<int> && !is_pointer<int>::value, "");
50
51 static_assert(is_lvalue_reference_v<int&>
52 && is_lvalue_reference<int&>::value, "");
53 static_assert(!is_lvalue_reference_v<int>
54 && !is_lvalue_reference<int>::value, "");
55
56 static_assert(is_rvalue_reference_v<int&&>
57 && is_rvalue_reference<int&&>::value, "");
58 static_assert(!is_rvalue_reference_v<int>
59 && !is_rvalue_reference<int>::value, "");
60
61 struct EmptyFinal final {};
62
63 static_assert(is_member_object_pointer_v<int (EmptyFinal::*)>
64 && is_member_object_pointer<int (EmptyFinal::*)>::value, "");
65 static_assert(!is_member_object_pointer_v<void*>
66 && !is_member_object_pointer<void*>::value, "");
67
68 static_assert(is_member_function_pointer_v<int (EmptyFinal::*)()>
69 && is_member_function_pointer<int (EmptyFinal::*)()>::value, "");
70 static_assert(!is_member_function_pointer_v<void*>
71 && !is_member_function_pointer<void*>::value, "");
72
73 enum Enum {};
74
75 static_assert(is_enum_v<Enum> && is_enum<Enum>::value, "");
76 static_assert(!is_enum_v<int> && !is_enum<int>::value, "");
77
78 union Union;
79
80 static_assert(is_union_v<Union> && is_union<Union>::value, "");
81 static_assert(!is_union_v<int> && !is_union<int>::value, "");
82
83 static_assert(is_class_v<EmptyFinal> && is_class<EmptyFinal>::value, "");
84 static_assert(!is_class_v<int> && !is_class<int>::value, "");
85
86 static_assert(is_function_v<void()> && is_function<void()>::value, "");
87 static_assert(!is_function_v<void(*)()> && !is_function<void(*)()>::value, "");
88
89 static_assert(is_reference_v<int&> && is_reference<int&>::value, "");
90 static_assert(!is_reference_v<int> && !is_reference<int>::value, "");
91
92 static_assert(is_arithmetic_v<int> && is_arithmetic<int>::value, "");
93 static_assert(!is_arithmetic_v<void*> && !is_arithmetic<void*>::value, "");
94
95 static_assert(is_fundamental_v<int> && is_fundamental<int>::value, "");
96 static_assert(!is_fundamental_v<EmptyFinal>
97 && !is_fundamental<EmptyFinal>::value, "");
98
99 static_assert(is_object_v<int> && is_object<int>::value, "");
100 static_assert(!is_object_v<int&> && !is_object<int&>::value, "");
101
102 static_assert(is_scalar_v<int> && is_scalar<int>::value, "");
103 static_assert(!is_scalar_v<int&> && !is_scalar<int&>::value, "");
104
105 static_assert(is_compound_v<EmptyFinal>
106 && is_compound<EmptyFinal>::value, "");
107 static_assert(!is_compound_v<int> && !is_compound<int>::value, "");
108
109 static_assert(is_member_pointer_v<int (EmptyFinal::*)>
110 && is_member_pointer<int (EmptyFinal::*)>::value, "");
111 static_assert(!is_member_pointer_v<void*>
112 && !is_member_pointer<void*>::value, "");
113
114 static_assert(is_const_v<const int> && is_const<const int>::value, "");
115 static_assert(!is_const_v<int> && !is_const<int>::value, "");
116
117 static_assert(is_volatile_v<volatile int>
118 && is_volatile<volatile int>::value, "");
119 static_assert(!is_volatile_v<int> && !is_volatile<int>::value, "");
120
121 struct NType
122 {
123 NType(int);
124 ~NType();
125 int i;
126 private:
127 NType(const NType&);
128 NType& operator=(const NType&);
129 int i2;
130 };
131
132 #pragma GCC diagnostic push
133 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
134 // Deprecated in C++26
135 static_assert(is_trivial_v<int> && is_trivial<int>::value, "");
136 static_assert(!is_trivial_v<NType> && !is_trivial<NType>::value, "");
137 #pragma GCC diagnostic pop
138
139 static_assert(is_trivially_copyable_v<int>
140 && is_trivially_copyable<int>::value, "");
141 static_assert(!is_trivially_copyable_v<NType>
142 && !is_trivially_copyable<NType>::value, "");
143
144 static_assert(is_standard_layout_v<int>
145 && is_standard_layout<int>::value, "");
146 static_assert(!is_standard_layout_v<NType>
147 && !is_standard_layout<NType>::value, "");
148
149 // Deprecated in C++20
150 static_assert(is_pod_v<int>
151 && is_pod<int>::value, "");
152 static_assert(!is_pod_v<NType>
153 && !is_pod<NType>::value, "");
154
155 #pragma GCC diagnostic push
156 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
157 static_assert(is_literal_type_v<int>
158 && is_literal_type<int>::value, "");
159 static_assert(!is_literal_type_v<NType>
160 && !is_literal_type<NType>::value, "");
161 #pragma GCC diagnostic pop
162
163 static_assert(is_empty_v<EmptyFinal>
164 && is_empty<EmptyFinal>::value, "");
165 static_assert(!is_empty_v<NType>
166 && !is_empty<NType>::value, "");
167
168 struct Abstract {protected: virtual ~Abstract() = 0;};
169 struct Poly : Abstract {virtual ~Poly();};
170
171 static_assert(is_polymorphic_v<Poly>
172 && is_polymorphic<Poly>::value, "");
173 static_assert(!is_polymorphic_v<EmptyFinal>
174 && !is_polymorphic<EmptyFinal>::value, "");
175
176
177
178 static_assert(is_abstract_v<Abstract>
179 && is_abstract<Abstract>::value, "");
180 static_assert(!is_abstract_v<EmptyFinal>
181 && !is_abstract<EmptyFinal>::value, "");
182
183 static_assert(is_final_v<EmptyFinal>
184 && is_final<EmptyFinal>::value, "");
185 static_assert(!is_final_v<Abstract>
186 && !is_final<Abstract>::value, "");
187
188 static_assert(is_signed_v<int> && is_signed<int>::value, "");
189 static_assert(!is_signed_v<unsigned int>
190 && !is_signed<unsigned int>::value, "");
191
192 static_assert(is_constructible_v<int, int>
193 && is_constructible<int, int>::value, "");
194 static_assert(!is_constructible_v<int, void*>
195 && !is_constructible<int, void*>::value, "");
196
197 static_assert(is_default_constructible_v<int>
198 && is_default_constructible<int>::value, "");
199 static_assert(!is_default_constructible_v<NType>
200 && !is_default_constructible<NType>::value, "");
201
202 static_assert(is_copy_constructible_v<int>
203 && is_copy_constructible<int>::value, "");
204 static_assert(!is_copy_constructible_v<NType>
205 && !is_copy_constructible<NType>::value, "");
206
207 static_assert(is_move_constructible_v<int>
208 && is_copy_constructible<int>::value, "");
209 static_assert(!is_move_constructible_v<NType>
210 && !is_copy_constructible<NType>::value, "");
211
212 static_assert(is_assignable_v<int&, int>
213 && is_assignable<int&, int>::value, "");
214 static_assert(!is_assignable_v<int, int>
215 && !is_assignable<int, int>::value, "");
216
217 static_assert(is_copy_assignable_v<int>
218 && is_copy_assignable<int>::value, "");
219 static_assert(!is_copy_assignable_v<NType>
220 && !is_copy_assignable<NType>::value, "");
221
222 static_assert(is_move_assignable_v<int>
223 && is_move_assignable<int>::value, "");
224 static_assert(!is_move_assignable_v<NType>
225 && !is_move_assignable<NType>::value, "");
226
227 static_assert(is_destructible_v<int>
228 && is_destructible<int>::value, "");
229 static_assert(!is_destructible_v<Abstract>
230 && !is_destructible<Abstract>::value, "");
231
232 static_assert(is_trivially_constructible_v<int, int>
233 && is_trivially_constructible<int, int>::value, "");
234 static_assert(!is_trivially_constructible_v<NType, NType>
235 && !is_trivially_constructible<NType, NType>::value, "");
236
237 static_assert(is_trivially_default_constructible_v<int>
238 && is_trivially_default_constructible<int>::value, "");
239 static_assert(!is_trivially_default_constructible_v<NType>
240 && !is_trivially_default_constructible<NType>::value, "");
241
242 static_assert(is_trivially_copy_constructible_v<int>
243 && is_trivially_copy_constructible<int>::value, "");
244 static_assert(!is_trivially_copy_constructible_v<NType>
245 && !is_trivially_copy_constructible<NType>::value, "");
246
247 static_assert(is_trivially_move_constructible_v<int>
248 && is_trivially_move_constructible<int>::value, "");
249 static_assert(!is_trivially_move_constructible_v<NType>
250 && !is_trivially_move_constructible<NType>::value, "");
251
252 static_assert(is_trivially_assignable_v<int&, int>
253 && is_trivially_assignable<int&, int>::value, "");
254 static_assert(!is_trivially_assignable_v<NType, NType>
255 && !is_trivially_assignable<NType, NType>::value, "");
256
257 static_assert(is_trivially_copy_assignable_v<int>
258 && is_trivially_copy_assignable<int>::value, "");
259 static_assert(!is_trivially_copy_assignable_v<NType>
260 && !is_trivially_copy_assignable<NType>::value, "");
261
262 static_assert(is_trivially_move_assignable_v<int>
263 && is_trivially_move_assignable<int>::value, "");
264 static_assert(!is_trivially_move_assignable_v<NType>
265 && !is_trivially_move_assignable<NType>::value, "");
266
267 static_assert(is_trivially_destructible_v<int>
268 && is_trivially_destructible<int>::value, "");
269 static_assert(!is_trivially_destructible_v<Abstract>
270 && !is_trivially_destructible<Abstract>::value, "");
271
272 static_assert(is_nothrow_constructible_v<int, int>
273 && is_nothrow_constructible<int, int>::value, "");
274 static_assert(!is_nothrow_constructible_v<NType, NType>
275 && !is_nothrow_constructible<NType, NType>::value, "");
276
277 static_assert(is_nothrow_default_constructible_v<int>
278 && is_nothrow_default_constructible<int>::value, "");
279 static_assert(!is_nothrow_default_constructible_v<NType>
280 && !is_nothrow_default_constructible<NType>::value, "");
281
282 static_assert(is_nothrow_copy_constructible_v<int>
283 && is_nothrow_copy_constructible<int>::value, "");
284 static_assert(!is_nothrow_copy_constructible_v<NType>
285 && !is_nothrow_copy_constructible<NType>::value, "");
286
287 static_assert(is_nothrow_move_constructible_v<int>
288 && is_nothrow_move_constructible<int>::value, "");
289 static_assert(!is_nothrow_move_constructible_v<NType>
290 && !is_nothrow_move_constructible<NType>::value, "");
291
292 static_assert(is_nothrow_assignable_v<int&, int>
293 && is_nothrow_assignable<int&, int>::value, "");
294 static_assert(!is_nothrow_assignable_v<NType, NType>
295 && !is_nothrow_assignable<NType, NType>::value, "");
296
297 static_assert(is_nothrow_copy_assignable_v<int>
298 && is_nothrow_copy_assignable<int>::value, "");
299 static_assert(!is_nothrow_copy_assignable_v<NType>
300 && !is_nothrow_copy_assignable<NType>::value, "");
301
302 static_assert(is_nothrow_move_assignable_v<int>
303 && is_nothrow_move_assignable<int>::value, "");
304 static_assert(!is_nothrow_move_assignable_v<NType>
305 && !is_nothrow_move_assignable<NType>::value, "");
306
307 static_assert(has_virtual_destructor_v<Abstract>
308 && has_virtual_destructor<Abstract>::value, "");
309 static_assert(!has_virtual_destructor_v<NType>
310 && !has_virtual_destructor<NType>::value, "");
311
312 static_assert(alignment_of_v<int> == alignof(int)
313 && alignment_of<int>::value == alignof(int) , "");
314
315 static_assert(rank_v<int[1][1]> == rank<int[1][1]>::value, "");
316
317 static_assert(extent_v<int[1][2], 1> == 2
318 && extent<int[1][2], 1>::value == 2, "");
319
320 static_assert(is_same_v<int, int> && is_same<int, int>::value, "");
321 static_assert(!is_same_v<int, char> && !is_same<int, char>::value, "");
322
323 static_assert(is_base_of_v<Abstract, Poly>
324 && is_base_of<Abstract, Poly>::value, "");
325 static_assert(!is_base_of_v<Abstract, NType>
326 && !is_base_of<Abstract, NType>::value, "");
327
328 static_assert(is_convertible_v<int&, const int&>
329 && is_convertible<int&, const int&>::value, "");
330 static_assert(!is_convertible_v<const int&, int&>
331 && !is_convertible<const int&, int&>::value, "");
332
333 static_assert(negation_v<false_type>, "");
334 static_assert(!negation_v<true_type>, "");
335 static_assert(conjunction_v<>, "");
336 static_assert(!disjunction_v<>, "");
337 static_assert(conjunction_v<true_type>, "");
338 static_assert(!conjunction_v<false_type>, "");
339 static_assert(disjunction_v<true_type>, "");
340 static_assert(!disjunction_v<false_type>, "");
341 static_assert(conjunction_v<true_type, true_type>, "");
342 static_assert(!conjunction_v<true_type, false_type>, "");
343 static_assert(disjunction_v<false_type, true_type>, "");
344 static_assert(!disjunction_v<false_type, false_type>, "");
345 static_assert(conjunction_v<true_type, true_type,
346 true_type>, "");
347 static_assert(!conjunction_v<true_type, true_type,
348 false_type>, "");
349 static_assert(disjunction_v<false_type, false_type,
350 true_type>, "");
351 static_assert(!disjunction_v<false_type, false_type,
352 false_type>, "");
353 #if __cpp_lib_reference_from_temporary >= 202202L
354 static_assert(std::reference_converts_from_temporary_v<int&&, int>
355 && std::reference_converts_from_temporary_v<const int&, int>
356 && !std::reference_converts_from_temporary_v<int&&, int&&>
357 && !std::reference_converts_from_temporary_v<const int&, int&&>
358 && std::reference_converts_from_temporary_v<int&&, long&&>
359 && std::reference_converts_from_temporary_v<int&&, long>, "");
360 static_assert(std::reference_constructs_from_temporary_v<int&&, int>
361 && std::reference_constructs_from_temporary_v<const int&, int>
362 && !std::reference_constructs_from_temporary_v<int&&, int&&>
363 && !std::reference_constructs_from_temporary_v<const int&, int&&>
364 && std::reference_constructs_from_temporary_v<int&&, long&&>
365 && std::reference_constructs_from_temporary_v<int&&, long>, "");
366 #endif