Debian Codegraph @

libstdc++-v3/testsuite/experimental/type_traits/value.cc

raw
1 // { dg-do compile { target c++14 } }
2 // { dg-additional-options "-Wno-deprecated" { target { c++2a } } }
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 <experimental/type_traits>
22
23 using std::true_type;
24 using std::false_type;
25 using std::nullptr_t;
26 using std::is_void;
27 using std::is_null_pointer;
28 using std::is_integral;
29 using std::is_floating_point;
30 using std::is_array;
31 using std::is_pointer;
32 using std::is_lvalue_reference;
33 using std::is_rvalue_reference;
34 using std::is_member_object_pointer;
35 using std::is_member_function_pointer;
36 using std::is_enum;
37 using std::is_union;
38 using std::is_class;
39 using std::is_function;
40 using std::is_reference;
41 using std::is_arithmetic;
42 using std::is_fundamental;
43 using std::is_object;
44 using std::is_scalar;
45 using std::is_compound;
46 using std::is_member_pointer;
47 using std::is_const;
48 using std::is_volatile;
49 using std::is_trivial;
50 using std::is_trivially_copyable;
51 using std::is_standard_layout;
52 using std::is_pod;
53 using std::is_literal_type;
54 using std::is_empty;
55 using std::is_polymorphic;
56 using std::is_abstract;
57 using std::is_final;
58 using std::is_signed;
59 using std::is_constructible;
60 using std::is_default_constructible;
61 using std::is_copy_constructible;
62 using std::is_move_constructible;
63 using std::is_assignable;
64 using std::is_copy_assignable;
65 using std::is_move_assignable;
66 using std::is_destructible;
67 using std::is_trivially_constructible;
68 using std::is_trivially_default_constructible;
69 using std::is_trivially_copy_constructible;
70 using std::is_trivially_move_constructible;
71 using std::is_trivially_assignable;
72 using std::is_trivially_copy_assignable;
73 using std::is_trivially_move_assignable;
74 using std::is_trivially_destructible;
75 using std::is_nothrow_constructible;
76 using std::is_nothrow_default_constructible;
77 using std::is_nothrow_copy_constructible;
78 using std::is_nothrow_move_constructible;
79 using std::is_nothrow_assignable;
80 using std::is_nothrow_copy_assignable;
81 using std::is_nothrow_move_assignable;
82 using std::is_nothrow_destructible;
83 using std::has_virtual_destructor;
84 using std::alignment_of;
85 using std::rank;
86 using std::extent;
87 using std::is_same;
88 using std::is_base_of;
89 using std::is_convertible;
90 using namespace std::experimental;
91
92 // These tests are rather simple, the front-end tests already test
93 // variable templates, and the library tests for the underlying
94 // traits are more elaborate. These are just simple sanity tests.
95
96 static_assert(is_void_v<void> && is_void<void>::value, "");
97 static_assert(!is_void_v<int> && !is_void<int>::value, "");
98
99 static_assert(is_null_pointer_v<nullptr_t>
100 && is_null_pointer<nullptr_t>::value, "");
101 static_assert(!is_null_pointer_v<void*>
102 && !is_null_pointer<void*>::value, "");
103
104 static_assert(is_integral_v<int> && is_integral<int>::value, "");
105 static_assert(!is_integral_v<int*> && !is_integral<int*>::value, "");
106
107 static_assert(is_floating_point_v<float>
108 && is_floating_point<float>::value, "");
109 static_assert(!is_floating_point_v<int>
110 && !is_floating_point<int>::value, "");
111
112 static_assert(is_array_v<char[42]> && is_array<char[42]>::value, "");
113 static_assert(!is_array_v<char*> && !is_array<char*>::value, "");
114
115 static_assert(is_pointer_v<int*> && is_pointer<int*>::value, "");
116 static_assert(!is_pointer_v<int> && !is_pointer<int>::value, "");
117
118 static_assert(is_lvalue_reference_v<int&>
119 && is_lvalue_reference<int&>::value, "");
120 static_assert(!is_lvalue_reference_v<int>
121 && !is_lvalue_reference<int>::value, "");
122
123 static_assert(is_rvalue_reference_v<int&&>
124 && is_rvalue_reference<int&&>::value, "");
125 static_assert(!is_rvalue_reference_v<int>
126 && !is_rvalue_reference<int>::value, "");
127
128 struct EmptyFinal final {};
129
130 static_assert(is_member_object_pointer_v<int (EmptyFinal::*)>
131 && is_member_object_pointer<int (EmptyFinal::*)>::value, "");
132 static_assert(!is_member_object_pointer_v<void*>
133 && !is_member_object_pointer<void*>::value, "");
134
135 static_assert(is_member_function_pointer_v<int (EmptyFinal::*)()>
136 && is_member_function_pointer<int (EmptyFinal::*)()>::value, "");
137 static_assert(!is_member_function_pointer_v<void*>
138 && !is_member_function_pointer<void*>::value, "");
139
140 enum Enum {};
141
142 static_assert(is_enum_v<Enum> && is_enum<Enum>::value, "");
143 static_assert(!is_enum_v<int> && !is_enum<int>::value, "");
144
145 union Union;
146
147 static_assert(is_union_v<Union> && is_union<Union>::value, "");
148 static_assert(!is_union_v<int> && !is_union<int>::value, "");
149
150 static_assert(is_class_v<EmptyFinal> && is_class<EmptyFinal>::value, "");
151 static_assert(!is_class_v<int> && !is_class<int>::value, "");
152
153 static_assert(is_function_v<void()> && is_function<void()>::value, "");
154 static_assert(!is_function_v<void(*)()> && !is_function<void(*)()>::value, "");
155
156 static_assert(is_reference_v<int&> && is_reference<int&>::value, "");
157 static_assert(!is_reference_v<int> && !is_reference<int>::value, "");
158
159 static_assert(is_arithmetic_v<int> && is_arithmetic<int>::value, "");
160 static_assert(!is_arithmetic_v<void*> && !is_arithmetic<void*>::value, "");
161
162 static_assert(is_fundamental_v<int> && is_fundamental<int>::value, "");
163 static_assert(!is_fundamental_v<EmptyFinal>
164 && !is_fundamental<EmptyFinal>::value, "");
165
166 static_assert(is_object_v<int> && is_object<int>::value, "");
167 static_assert(!is_object_v<int&> && !is_object<int&>::value, "");
168
169 static_assert(is_scalar_v<int> && is_scalar<int>::value, "");
170 static_assert(!is_scalar_v<int&> && !is_scalar<int&>::value, "");
171
172 static_assert(is_compound_v<EmptyFinal>
173 && is_compound<EmptyFinal>::value, "");
174 static_assert(!is_compound_v<int> && !is_compound<int>::value, "");
175
176 static_assert(is_member_pointer_v<int (EmptyFinal::*)>
177 && is_member_pointer<int (EmptyFinal::*)>::value, "");
178 static_assert(!is_member_pointer_v<void*>
179 && !is_member_pointer<void*>::value, "");
180
181 static_assert(is_const_v<const int> && is_const<const int>::value, "");
182 static_assert(!is_const_v<int> && !is_const<int>::value, "");
183
184 static_assert(is_volatile_v<volatile int>
185 && is_volatile<volatile int>::value, "");
186 static_assert(!is_volatile_v<int> && !is_volatile<int>::value, "");
187
188 struct NType
189 {
190 NType(int);
191 ~NType();
192 int i;
193 private:
194 NType(const NType&);
195 NType& operator=(const NType&);
196 int i2;
197 };
198
199 #pragma GCC diagnostic push
200 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
201 // Deprecated in C++26
202 static_assert(is_trivial_v<int> && is_trivial<int>::value, "");
203 static_assert(!is_trivial_v<NType> && !is_trivial<NType>::value, "");
204 #pragma GCC diagnostic pop
205
206 static_assert(is_trivially_copyable_v<int>
207 && is_trivially_copyable<int>::value, "");
208 static_assert(!is_trivially_copyable_v<NType>
209 && !is_trivially_copyable<NType>::value, "");
210
211 static_assert(is_standard_layout_v<int>
212 && is_standard_layout<int>::value, "");
213 static_assert(!is_standard_layout_v<NType>
214 && !is_standard_layout<NType>::value, "");
215
216 static_assert(is_pod_v<int>
217 && is_pod<int>::value, "");
218 static_assert(!is_pod_v<NType>
219 && !is_pod<NType>::value, "");
220
221 #pragma GCC diagnostic push
222 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
223 static_assert(is_literal_type_v<int>
224 && is_literal_type<int>::value, "");
225 static_assert(!is_literal_type_v<NType>
226 && !is_literal_type<NType>::value, "");
227 #pragma GCC diagnostic pop
228
229 static_assert(is_empty_v<EmptyFinal>
230 && is_empty<EmptyFinal>::value, "");
231 static_assert(!is_empty_v<NType>
232 && !is_empty<NType>::value, "");
233
234 struct Abstract {protected: virtual ~Abstract() = 0;};
235 struct Poly : Abstract {virtual ~Poly();};
236
237 static_assert(is_polymorphic_v<Poly>
238 && is_polymorphic<Poly>::value, "");
239 static_assert(!is_polymorphic_v<EmptyFinal>
240 && !is_polymorphic<EmptyFinal>::value, "");
241
242
243
244 static_assert(is_abstract_v<Abstract>
245 && is_abstract<Abstract>::value, "");
246 static_assert(!is_abstract_v<EmptyFinal>
247 && !is_abstract<EmptyFinal>::value, "");
248
249 static_assert(is_final_v<EmptyFinal>
250 && is_final<EmptyFinal>::value, "");
251 static_assert(!is_final_v<Abstract>
252 && !is_final<Abstract>::value, "");
253
254 static_assert(is_signed_v<int> && is_signed<int>::value, "");
255 static_assert(!is_signed_v<unsigned int>
256 && !is_signed<unsigned int>::value, "");
257
258 static_assert(is_constructible_v<int, int>
259 && is_constructible<int, int>::value, "");
260 static_assert(!is_constructible_v<int, void*>
261 && !is_constructible<int, void*>::value, "");
262
263 static_assert(is_default_constructible_v<int>
264 && is_default_constructible<int>::value, "");
265 static_assert(!is_default_constructible_v<NType>
266 && !is_default_constructible<NType>::value, "");
267
268 static_assert(is_copy_constructible_v<int>
269 && is_copy_constructible<int>::value, "");
270 static_assert(!is_copy_constructible_v<NType>
271 && !is_copy_constructible<NType>::value, "");
272
273 static_assert(is_move_constructible_v<int>
274 && is_copy_constructible<int>::value, "");
275 static_assert(!is_move_constructible_v<NType>
276 && !is_copy_constructible<NType>::value, "");
277
278 static_assert(is_assignable_v<int&, int>
279 && is_assignable<int&, int>::value, "");
280 static_assert(!is_assignable_v<int, int>
281 && !is_assignable<int, int>::value, "");
282
283 static_assert(is_copy_assignable_v<int>
284 && is_copy_assignable<int>::value, "");
285 static_assert(!is_copy_assignable_v<NType>
286 && !is_copy_assignable<NType>::value, "");
287
288 static_assert(is_move_assignable_v<int>
289 && is_move_assignable<int>::value, "");
290 static_assert(!is_move_assignable_v<NType>
291 && !is_move_assignable<NType>::value, "");
292
293 static_assert(is_destructible_v<int>
294 && is_destructible<int>::value, "");
295 static_assert(!is_destructible_v<Abstract>
296 && !is_destructible<Abstract>::value, "");
297
298 static_assert(is_trivially_constructible_v<int, int>
299 && is_trivially_constructible<int, int>::value, "");
300 static_assert(!is_trivially_constructible_v<NType, NType>
301 && !is_trivially_constructible<NType, NType>::value, "");
302
303 static_assert(is_trivially_default_constructible_v<int>
304 && is_trivially_default_constructible<int>::value, "");
305 static_assert(!is_trivially_default_constructible_v<NType>
306 && !is_trivially_default_constructible<NType>::value, "");
307
308 static_assert(is_trivially_copy_constructible_v<int>
309 && is_trivially_copy_constructible<int>::value, "");
310 static_assert(!is_trivially_copy_constructible_v<NType>
311 && !is_trivially_copy_constructible<NType>::value, "");
312
313 static_assert(is_trivially_move_constructible_v<int>
314 && is_trivially_move_constructible<int>::value, "");
315 static_assert(!is_trivially_move_constructible_v<NType>
316 && !is_trivially_move_constructible<NType>::value, "");
317
318 static_assert(is_trivially_assignable_v<int&, int>
319 && is_trivially_assignable<int&, int>::value, "");
320 static_assert(!is_trivially_assignable_v<NType, NType>
321 && !is_trivially_assignable<NType, NType>::value, "");
322
323 static_assert(is_trivially_copy_assignable_v<int>
324 && is_trivially_copy_assignable<int>::value, "");
325 static_assert(!is_trivially_copy_assignable_v<NType>
326 && !is_trivially_copy_assignable<NType>::value, "");
327
328 static_assert(is_trivially_move_assignable_v<int>
329 && is_trivially_move_assignable<int>::value, "");
330 static_assert(!is_trivially_move_assignable_v<NType>
331 && !is_trivially_move_assignable<NType>::value, "");
332
333 static_assert(is_trivially_destructible_v<int>
334 && is_trivially_destructible<int>::value, "");
335 static_assert(!is_trivially_destructible_v<Abstract>
336 && !is_trivially_destructible<Abstract>::value, "");
337
338 static_assert(is_nothrow_constructible_v<int, int>
339 && is_nothrow_constructible<int, int>::value, "");
340 static_assert(!is_nothrow_constructible_v<NType, NType>
341 && !is_nothrow_constructible<NType, NType>::value, "");
342
343 static_assert(is_nothrow_default_constructible_v<int>
344 && is_nothrow_default_constructible<int>::value, "");
345 static_assert(!is_nothrow_default_constructible_v<NType>
346 && !is_nothrow_default_constructible<NType>::value, "");
347
348 static_assert(is_nothrow_copy_constructible_v<int>
349 && is_nothrow_copy_constructible<int>::value, "");
350 static_assert(!is_nothrow_copy_constructible_v<NType>
351 && !is_nothrow_copy_constructible<NType>::value, "");
352
353 static_assert(is_nothrow_move_constructible_v<int>
354 && is_nothrow_move_constructible<int>::value, "");
355 static_assert(!is_nothrow_move_constructible_v<NType>
356 && !is_nothrow_move_constructible<NType>::value, "");
357
358 static_assert(is_nothrow_assignable_v<int&, int>
359 && is_nothrow_assignable<int&, int>::value, "");
360 static_assert(!is_nothrow_assignable_v<NType, NType>
361 && !is_nothrow_assignable<NType, NType>::value, "");
362
363 static_assert(is_nothrow_copy_assignable_v<int>
364 && is_nothrow_copy_assignable<int>::value, "");
365 static_assert(!is_nothrow_copy_assignable_v<NType>
366 && !is_nothrow_copy_assignable<NType>::value, "");
367
368 static_assert(is_nothrow_move_assignable_v<int>
369 && is_nothrow_move_assignable<int>::value, "");
370 static_assert(!is_nothrow_move_assignable_v<NType>
371 && !is_nothrow_move_assignable<NType>::value, "");
372
373 static_assert(has_virtual_destructor_v<Abstract>
374 && has_virtual_destructor<Abstract>::value, "");
375 static_assert(!has_virtual_destructor_v<NType>
376 && !has_virtual_destructor<NType>::value, "");
377
378 static_assert(alignment_of_v<int> == alignof(int)
379 && alignment_of<int>::value == alignof(int) , "");
380
381 static_assert(rank_v<int[1][1]> == rank<int[1][1]>::value, "");
382
383 static_assert(extent_v<int[1][2], 1> == 2
384 && extent<int[1][2], 1>::value == 2, "");
385
386 static_assert(is_same_v<int, int> && is_same<int, int>::value, "");
387 static_assert(!is_same_v<int, char> && !is_same<int, char>::value, "");
388
389 static_assert(is_base_of_v<Abstract, Poly>
390 && is_base_of<Abstract, Poly>::value, "");
391 static_assert(!is_base_of_v<Abstract, NType>
392 && !is_base_of<Abstract, NType>::value, "");
393
394 static_assert(is_convertible_v<int&, const int&>
395 && is_convertible<int&, const int&>::value, "");
396 static_assert(!is_convertible_v<const int&, int&>
397 && !is_convertible<const int&, int&>::value, "");
398
399 static_assert(negation_v<false_type>, "");
400 static_assert(!negation_v<true_type>, "");
401 static_assert(conjunction_v<>, "");
402 static_assert(!disjunction_v<>, "");
403 static_assert(conjunction_v<true_type>, "");
404 static_assert(!conjunction_v<false_type>, "");
405 static_assert(disjunction_v<true_type>, "");
406 static_assert(!disjunction_v<false_type>, "");
407 static_assert(conjunction_v<true_type, true_type>, "");
408 static_assert(!conjunction_v<true_type, false_type>, "");
409 static_assert(disjunction_v<false_type, true_type>, "");
410 static_assert(!disjunction_v<false_type, false_type>, "");
411 static_assert(conjunction_v<true_type, true_type,
412 true_type>, "");
413 static_assert(!conjunction_v<true_type, true_type,
414 false_type>, "");
415 static_assert(disjunction_v<false_type, false_type,
416 true_type>, "");
417 static_assert(!disjunction_v<false_type, false_type,
418 false_type>, "");