Debian Codegraph @

libstdc++-v3/include/tr1/type_traits

raw
1 // TR1 type_traits -*- C++ -*-
2
3 // Copyright (C) 2004-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file tr1/type_traits
26 * This is a TR1 C++ Library header.
27 */
28
29 #ifndef _GLIBCXX_TR1_TYPE_TRAITS
30 #define _GLIBCXX_TR1_TYPE_TRAITS 1
31
32 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
34 #endif
35
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Wc++11-extensions"
38
39 #include <bits/requires_hosted.h> // TR1
40
41 #include <bits/c++config.h>
42
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 namespace tr1
48 {
49 /**
50 * @addtogroup metaprogramming
51 * @{
52 */
53
54 struct __sfinae_types
55 {
56 typedef char __one;
57 typedef struct { char __arr[2]; } __two;
58 };
59
60 #define _DEFINE_SPEC_0_HELPER \
61 template<>
62
63 #define _DEFINE_SPEC_1_HELPER \
64 template<typename _Tp>
65
66 #define _DEFINE_SPEC_2_HELPER \
67 template<typename _Tp, typename _Cp>
68
69 #define _DEFINE_SPEC(_Order, _Trait, _Type, _Value) \
70 _DEFINE_SPEC_##_Order##_HELPER \
71 struct _Trait<_Type> \
72 : public integral_constant<bool, _Value> { };
73
74 // helper classes [4.3].
75
76 /// integral_constant
77 template<typename _Tp, _Tp __v>
78 struct integral_constant
79 {
80 static const _Tp value = __v;
81 typedef _Tp value_type;
82 typedef integral_constant<_Tp, __v> type;
83 };
84
85 /// typedef for true_type
86 typedef integral_constant<bool, true> true_type;
87
88 /// typedef for false_type
89 typedef integral_constant<bool, false> false_type;
90
91 template<typename _Tp, _Tp __v>
92 const _Tp integral_constant<_Tp, __v>::value;
93
94 /// remove_cv
95 template<typename>
96 struct remove_cv;
97
98 template<typename>
99 struct __is_void_helper
100 : public false_type { };
101 _DEFINE_SPEC(0, __is_void_helper, void, true)
102
103 // primary type categories [4.5.1].
104
105 /// is_void
106 template<typename _Tp>
107 struct is_void
108 : public integral_constant<bool, (__is_void_helper<typename
109 remove_cv<_Tp>::type>::value)>
110 { };
111
112 template<typename>
113 struct __is_integral_helper
114 : public false_type { };
115 _DEFINE_SPEC(0, __is_integral_helper, bool, true)
116 _DEFINE_SPEC(0, __is_integral_helper, char, true)
117 _DEFINE_SPEC(0, __is_integral_helper, signed char, true)
118 _DEFINE_SPEC(0, __is_integral_helper, unsigned char, true)
119 _DEFINE_SPEC(0, __is_integral_helper, wchar_t, true)
120 _DEFINE_SPEC(0, __is_integral_helper, short, true)
121 _DEFINE_SPEC(0, __is_integral_helper, unsigned short, true)
122 _DEFINE_SPEC(0, __is_integral_helper, int, true)
123 _DEFINE_SPEC(0, __is_integral_helper, unsigned int, true)
124 _DEFINE_SPEC(0, __is_integral_helper, long, true)
125 _DEFINE_SPEC(0, __is_integral_helper, unsigned long, true)
126 #pragma GCC diagnostic push
127 #pragma GCC diagnostic ignored "-Wlong-long"
128 _DEFINE_SPEC(0, __is_integral_helper, long long, true)
129 _DEFINE_SPEC(0, __is_integral_helper, unsigned long long, true)
130 #pragma GCC diagnostic pop
131
132 /// is_integral
133 template<typename _Tp>
134 struct is_integral
135 : public integral_constant<bool, (__is_integral_helper<typename
136 remove_cv<_Tp>::type>::value)>
137 { };
138
139 template<typename>
140 struct __is_floating_point_helper
141 : public false_type { };
142 _DEFINE_SPEC(0, __is_floating_point_helper, float, true)
143 _DEFINE_SPEC(0, __is_floating_point_helper, double, true)
144 _DEFINE_SPEC(0, __is_floating_point_helper, long double, true)
145
146 /// is_floating_point
147 template<typename _Tp>
148 struct is_floating_point
149 : public integral_constant<bool, (__is_floating_point_helper<typename
150 remove_cv<_Tp>::type>::value)>
151 { };
152
153 /// is_array
154 template<typename>
155 struct is_array
156 : public false_type { };
157
158 template<typename _Tp, std::size_t _Size>
159 struct is_array<_Tp[_Size]>
160 : public true_type { };
161
162 template<typename _Tp>
163 struct is_array<_Tp[]>
164 : public true_type { };
165
166 template<typename>
167 struct __is_pointer_helper
168 : public false_type { };
169 _DEFINE_SPEC(1, __is_pointer_helper, _Tp*, true)
170
171 /// is_pointer
172 template<typename _Tp>
173 struct is_pointer
174 : public integral_constant<bool, (__is_pointer_helper<typename
175 remove_cv<_Tp>::type>::value)>
176 { };
177
178 /// is_reference
179 template<typename _Tp>
180 struct is_reference;
181
182 /// is_function
183 template<typename _Tp>
184 struct is_function;
185
186 template<typename>
187 struct __is_member_object_pointer_helper
188 : public false_type { };
189 _DEFINE_SPEC(2, __is_member_object_pointer_helper, _Tp _Cp::*,
190 !is_function<_Tp>::value)
191
192 /// is_member_object_pointer
193 template<typename _Tp>
194 struct is_member_object_pointer
195 : public integral_constant<bool, (__is_member_object_pointer_helper<
196 typename remove_cv<_Tp>::type>::value)>
197 { };
198
199 template<typename>
200 struct __is_member_function_pointer_helper
201 : public false_type { };
202 _DEFINE_SPEC(2, __is_member_function_pointer_helper, _Tp _Cp::*,
203 is_function<_Tp>::value)
204
205 /// is_member_function_pointer
206 template<typename _Tp>
207 struct is_member_function_pointer
208 : public integral_constant<bool, (__is_member_function_pointer_helper<
209 typename remove_cv<_Tp>::type>::value)>
210 { };
211
212 /// is_enum
213 template<typename _Tp>
214 struct is_enum
215 : public integral_constant<bool, __is_enum(_Tp)>
216 { };
217
218 /// is_union
219 template<typename _Tp>
220 struct is_union
221 : public integral_constant<bool, __is_union(_Tp)>
222 { };
223
224 /// is_class
225 template<typename _Tp>
226 struct is_class
227 : public integral_constant<bool, __is_class(_Tp)>
228 { };
229
230 /// is_function
231 template<typename>
232 struct is_function
233 : public false_type { };
234 template<typename _Res, typename... _ArgTypes>
235 struct is_function<_Res(_ArgTypes...)>
236 : public true_type { };
237 template<typename _Res, typename... _ArgTypes>
238 struct is_function<_Res(_ArgTypes..., ...)> : public true_type { };
239 template<typename _Res, typename... _ArgTypes>
240 struct is_function<_Res(_ArgTypes...) const>
241 : public true_type { };
242 template<typename _Res, typename... _ArgTypes>
243 struct is_function<_Res(_ArgTypes..., ...) const>
244 : public true_type { };
245 template<typename _Res, typename... _ArgTypes>
246 struct is_function<_Res(_ArgTypes...) volatile>
247 : public true_type { };
248 template<typename _Res, typename... _ArgTypes>
249 struct is_function<_Res(_ArgTypes..., ...) volatile>
250 : public true_type { };
251 template<typename _Res, typename... _ArgTypes>
252 struct is_function<_Res(_ArgTypes...) const volatile>
253 : public true_type { };
254 template<typename _Res, typename... _ArgTypes>
255 struct is_function<_Res(_ArgTypes..., ...) const volatile>
256 : public true_type { };
257
258 // composite type traits [4.5.2].
259
260 /// is_arithmetic
261 template<typename _Tp>
262 struct is_arithmetic
263 : public integral_constant<bool, (is_integral<_Tp>::value
264 || is_floating_point<_Tp>::value)>
265 { };
266
267 /// is_fundamental
268 template<typename _Tp>
269 struct is_fundamental
270 : public integral_constant<bool, (is_arithmetic<_Tp>::value
271 || is_void<_Tp>::value)>
272 { };
273
274 /// is_object
275 template<typename _Tp>
276 struct is_object
277 : public integral_constant<bool, !(is_function<_Tp>::value
278 || is_reference<_Tp>::value
279 || is_void<_Tp>::value)>
280 { };
281
282 /// is_member_pointer
283 template<typename _Tp>
284 struct is_member_pointer;
285
286 /// is_scalar
287 template<typename _Tp>
288 struct is_scalar
289 : public integral_constant<bool, (is_arithmetic<_Tp>::value
290 || is_enum<_Tp>::value
291 || is_pointer<_Tp>::value
292 || is_member_pointer<_Tp>::value)>
293 { };
294
295 /// is_compound
296 template<typename _Tp>
297 struct is_compound
298 : public integral_constant<bool, !is_fundamental<_Tp>::value> { };
299
300 /// is_member_pointer
301 template<typename _Tp>
302 struct __is_member_pointer_helper
303 : public false_type { };
304 _DEFINE_SPEC(2, __is_member_pointer_helper, _Tp _Cp::*, true)
305
306 template<typename _Tp>
307 struct is_member_pointer
308 : public integral_constant<bool, (__is_member_pointer_helper<
309 typename remove_cv<_Tp>::type>::value)>
310 { };
311
312 // type properties [4.5.3].
313 /// is_const
314 template<typename>
315 struct is_const
316 : public false_type { };
317
318 template<typename _Tp>
319 struct is_const<_Tp const>
320 : public true_type { };
321
322 /// is_volatile
323 template<typename>
324 struct is_volatile
325 : public false_type { };
326
327 template<typename _Tp>
328 struct is_volatile<_Tp volatile>
329 : public true_type { };
330
331 /// is_empty
332 template<typename _Tp>
333 struct is_empty
334 : public integral_constant<bool, __is_empty(_Tp)>
335 { };
336
337 /// is_polymorphic
338 template<typename _Tp>
339 struct is_polymorphic
340 : public integral_constant<bool, __is_polymorphic(_Tp)>
341 { };
342
343 /// is_abstract
344 template<typename _Tp>
345 struct is_abstract
346 : public integral_constant<bool, __is_abstract(_Tp)>
347 { };
348
349 /// has_virtual_destructor
350 template<typename _Tp>
351 struct has_virtual_destructor
352 : public integral_constant<bool, __has_virtual_destructor(_Tp)>
353 { };
354
355 /// alignment_of
356 template<typename _Tp>
357 struct alignment_of
358 : public integral_constant<std::size_t, __alignof__(_Tp)> { };
359
360 /// rank
361 template<typename>
362 struct rank
363 : public integral_constant<std::size_t, 0> { };
364
365 template<typename _Tp, std::size_t _Size>
366 struct rank<_Tp[_Size]>
367 : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { };
368
369 template<typename _Tp>
370 struct rank<_Tp[]>
371 : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { };
372
373 /// extent
374 template<typename, unsigned _Uint = 0>
375 struct extent
376 : public integral_constant<std::size_t, 0> { };
377
378 template<typename _Tp, unsigned _Uint, std::size_t _Size>
379 struct extent<_Tp[_Size], _Uint>
380 : public integral_constant<std::size_t,
381 _Uint == 0 ? _Size : extent<_Tp,
382 _Uint - 1>::value>
383 { };
384
385 template<typename _Tp, unsigned _Uint>
386 struct extent<_Tp[], _Uint>
387 : public integral_constant<std::size_t,
388 _Uint == 0 ? 0 : extent<_Tp,
389 _Uint - 1>::value>
390 { };
391
392 // relationships between types [4.6].
393
394 /// is_same
395 template<typename, typename>
396 struct is_same
397 : public false_type { };
398
399 template<typename _Tp>
400 struct is_same<_Tp, _Tp>
401 : public true_type { };
402
403 // const-volatile modifications [4.7.1].
404
405 /// remove_const
406 template<typename _Tp>
407 struct remove_const
408 { typedef _Tp type; };
409
410 template<typename _Tp>
411 struct remove_const<_Tp const>
412 { typedef _Tp type; };
413
414 /// remove_volatile
415 template<typename _Tp>
416 struct remove_volatile
417 { typedef _Tp type; };
418
419 template<typename _Tp>
420 struct remove_volatile<_Tp volatile>
421 { typedef _Tp type; };
422
423 /// remove_cv
424 template<typename _Tp>
425 struct remove_cv
426 {
427 typedef typename
428 remove_const<typename remove_volatile<_Tp>::type>::type type;
429 };
430
431 /// add_const
432 template<typename _Tp>
433 struct add_const
434 { typedef _Tp const type; };
435
436 /// add_volatile
437 template<typename _Tp>
438 struct add_volatile
439 { typedef _Tp volatile type; };
440
441 /// add_cv
442 template<typename _Tp>
443 struct add_cv
444 {
445 typedef typename
446 add_const<typename add_volatile<_Tp>::type>::type type;
447 };
448
449 // array modifications [4.7.3].
450
451 /// remove_extent
452 template<typename _Tp>
453 struct remove_extent
454 { typedef _Tp type; };
455
456 template<typename _Tp, std::size_t _Size>
457 struct remove_extent<_Tp[_Size]>
458 { typedef _Tp type; };
459
460 template<typename _Tp>
461 struct remove_extent<_Tp[]>
462 { typedef _Tp type; };
463
464 /// remove_all_extents
465 template<typename _Tp>
466 struct remove_all_extents
467 { typedef _Tp type; };
468
469 template<typename _Tp, std::size_t _Size>
470 struct remove_all_extents<_Tp[_Size]>
471 { typedef typename remove_all_extents<_Tp>::type type; };
472
473 template<typename _Tp>
474 struct remove_all_extents<_Tp[]>
475 { typedef typename remove_all_extents<_Tp>::type type; };
476
477 // pointer modifications [4.7.4].
478
479 template<typename _Tp, typename>
480 struct __remove_pointer_helper
481 { typedef _Tp type; };
482
483 template<typename _Tp, typename _Up>
484 struct __remove_pointer_helper<_Tp, _Up*>
485 { typedef _Up type; };
486
487 /// remove_pointer
488 template<typename _Tp>
489 struct remove_pointer
490 : public __remove_pointer_helper<_Tp, typename remove_cv<_Tp>::type>
491 { };
492
493 template<typename>
494 struct remove_reference;
495
496 /// add_pointer
497 template<typename _Tp>
498 struct add_pointer
499 { typedef typename remove_reference<_Tp>::type* type; };
500
501 template<typename>
502 struct is_reference
503 : public false_type { };
504
505 template<typename _Tp>
506 struct is_reference<_Tp&>
507 : public true_type { };
508
509 template<typename _Tp>
510 struct is_pod
511 : public integral_constant<bool, __is_pod(_Tp) || is_void<_Tp>::value>
512 { };
513
514 template<typename _Tp>
515 struct has_trivial_constructor
516 : public integral_constant<bool, is_pod<_Tp>::value>
517 { };
518
519 template<typename _Tp>
520 struct has_trivial_copy
521 : public integral_constant<bool, is_pod<_Tp>::value>
522 { };
523
524 template<typename _Tp>
525 struct has_trivial_assign
526 : public integral_constant<bool, is_pod<_Tp>::value>
527 { };
528
529 template<typename _Tp>
530 struct has_trivial_destructor
531 : public integral_constant<bool, is_pod<_Tp>::value>
532 { };
533
534 template<typename _Tp>
535 struct has_nothrow_constructor
536 : public integral_constant<bool, is_pod<_Tp>::value>
537 { };
538
539 template<typename _Tp>
540 struct has_nothrow_copy
541 : public integral_constant<bool, is_pod<_Tp>::value>
542 { };
543
544 template<typename _Tp>
545 struct has_nothrow_assign
546 : public integral_constant<bool, is_pod<_Tp>::value>
547 { };
548
549 #pragma GCC diagnostic push
550 #pragma GCC diagnostic ignored "-Wlong-long"
551 template<typename>
552 struct __is_signed_helper
553 : public false_type { };
554 _DEFINE_SPEC(0, __is_signed_helper, signed char, true)
555 _DEFINE_SPEC(0, __is_signed_helper, short, true)
556 _DEFINE_SPEC(0, __is_signed_helper, int, true)
557 _DEFINE_SPEC(0, __is_signed_helper, long, true)
558 _DEFINE_SPEC(0, __is_signed_helper, long long, true)
559
560 template<typename _Tp>
561 struct is_signed
562 : public integral_constant<bool, (__is_signed_helper<typename
563 remove_cv<_Tp>::type>::value)>
564 { };
565
566 template<typename>
567 struct __is_unsigned_helper
568 : public false_type { };
569 _DEFINE_SPEC(0, __is_unsigned_helper, unsigned char, true)
570 _DEFINE_SPEC(0, __is_unsigned_helper, unsigned short, true)
571 _DEFINE_SPEC(0, __is_unsigned_helper, unsigned int, true)
572 _DEFINE_SPEC(0, __is_unsigned_helper, unsigned long, true)
573 _DEFINE_SPEC(0, __is_unsigned_helper, unsigned long long, true)
574 #pragma GCC diagnostic pop
575
576 template<typename _Tp>
577 struct is_unsigned
578 : public integral_constant<bool, (__is_unsigned_helper<typename
579 remove_cv<_Tp>::type>::value)>
580 { };
581
582 template<typename _Base, typename _Derived>
583 struct __is_base_of_helper
584 {
585 typedef typename remove_cv<_Base>::type _NoCv_Base;
586 typedef typename remove_cv<_Derived>::type _NoCv_Derived;
587 static const bool __value = (is_same<_Base, _Derived>::value
588 || (__is_base_of(_Base, _Derived)
589 && !is_same<_NoCv_Base,
590 _NoCv_Derived>::value));
591 };
592
593 template<typename _Base, typename _Derived>
594 struct is_base_of
595 : public integral_constant<bool,
596 __is_base_of_helper<_Base, _Derived>::__value>
597 { };
598
599 template<typename _From, typename _To>
600 struct __is_convertible_simple
601 : public __sfinae_types
602 {
603 private:
604 static __one __test(_To);
605 static __two __test(...);
606 static _From __makeFrom();
607
608 public:
609 static const bool __value = sizeof(__test(__makeFrom())) == 1;
610 };
611
612 template<typename _Tp>
613 struct add_reference;
614
615 template<typename _Tp>
616 struct __is_int_or_cref
617 {
618 typedef typename remove_reference<_Tp>::type __rr_Tp;
619 static const bool __value = (is_integral<_Tp>::value
620 || (is_integral<__rr_Tp>::value
621 && is_const<__rr_Tp>::value
622 && !is_volatile<__rr_Tp>::value));
623 };
624
625 template<typename _From, typename _To,
626 bool = (is_void<_From>::value || is_void<_To>::value
627 || is_function<_To>::value || is_array<_To>::value
628 // This special case is here only to avoid warnings.
629 || (is_floating_point<typename
630 remove_reference<_From>::type>::value
631 && __is_int_or_cref<_To>::__value))>
632 struct __is_convertible_helper
633 {
634 // "An imaginary lvalue of type From...".
635 static const bool __value = (__is_convertible_simple<typename
636 add_reference<_From>::type, _To>::__value);
637 };
638
639 template<typename _From, typename _To>
640 struct __is_convertible_helper<_From, _To, true>
641 { static const bool __value = (is_void<_To>::value
642 || (__is_int_or_cref<_To>::__value
643 && !is_void<_From>::value)); };
644
645 template<typename _From, typename _To>
646 struct is_convertible
647 : public integral_constant<bool,
648 __is_convertible_helper<_From, _To>::__value>
649 { };
650
651 // reference modifications [4.7.2].
652 template<typename _Tp>
653 struct remove_reference
654 { typedef _Tp type; };
655
656 template<typename _Tp>
657 struct remove_reference<_Tp&>
658 { typedef _Tp type; };
659
660 // NB: Careful with reference to void.
661 template<typename _Tp, bool = (is_void<_Tp>::value
662 || is_reference<_Tp>::value)>
663 struct __add_reference_helper
664 { typedef _Tp& type; };
665
666 template<typename _Tp>
667 struct __add_reference_helper<_Tp, true>
668 { typedef _Tp type; };
669
670 template<typename _Tp>
671 struct add_reference
672 : public __add_reference_helper<_Tp>
673 { };
674
675 // other transformations [4.8].
676 template<std::size_t _Len, std::size_t _Align>
677 struct aligned_storage
678 {
679 union type
680 {
681 unsigned char __data[_Len];
682 struct __attribute__((__aligned__((_Align)))) { } __align;
683 };
684 };
685
686 #undef _DEFINE_SPEC_0_HELPER
687 #undef _DEFINE_SPEC_1_HELPER
688 #undef _DEFINE_SPEC_2_HELPER
689 #undef _DEFINE_SPEC
690
691 /// @} group metaprogramming
692 }
693
694 _GLIBCXX_END_NAMESPACE_VERSION
695 }
696
697 #pragma GCC diagnostic pop
698 #endif // _GLIBCXX_TR1_TYPE_TRAITS