OSDN Git Service

2011-04-13 Daniel Krugler <daniel.kruegler@googlemail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / type_traits
1 // C++0x type_traits -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011 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 include/type_traits
26  *  This is a Standard C++ Library header.
27  */
28
29 #ifndef _GLIBCXX_TYPE_TRAITS
30 #define _GLIBCXX_TYPE_TRAITS 1
31
32 #pragma GCC system_header
33
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
36 #else
37
38 #include <bits/c++config.h>
39
40 namespace std _GLIBCXX_VISIBILITY(default)
41 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43
44   /**
45    * @addtogroup metaprogramming
46    * @{
47    */
48   struct __sfinae_types
49   {
50     typedef char __one;
51     typedef struct { char __arr[2]; } __two;
52   };
53
54   // Meta programming helper types.
55
56   template<bool, typename, typename>
57     struct conditional;
58
59   template<typename _Tp, _Tp>
60     struct integral_constant;
61
62   template<typename, typename, typename...>
63     struct __or_;
64
65   template<typename _B1, typename _B2>
66     struct __or_<_B1, _B2>
67     : public conditional<_B1::value, _B1, _B2>::type
68     { };
69
70   template<typename _B1, typename _B2, typename _B3, typename... _Bn>
71     struct __or_<_B1, _B2, _B3, _Bn...>
72     : public conditional<_B1::value, _B1, __or_<_B2, _B3, _Bn...>>::type
73     { };
74
75   template<typename, typename, typename...>
76     struct __and_;
77
78   template<typename _B1, typename _B2>
79     struct __and_<_B1, _B2>
80     : public conditional<_B1::value, _B2, _B1>::type
81     { };
82
83   template<typename _B1, typename _B2, typename _B3, typename... _Bn>
84     struct __and_<_B1, _B2, _B3, _Bn...>
85     : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type
86     { };
87
88   template<typename _Pp>
89     struct __not_
90     : public integral_constant<bool, !_Pp::value>
91     { };
92
93   // helper class.
94
95   /// integral_constant
96   template<typename _Tp, _Tp __v>
97     struct integral_constant
98     {
99       static constexpr _Tp                  value = __v;
100       typedef _Tp                           value_type;
101       typedef integral_constant<_Tp, __v>   type;
102       constexpr operator value_type() { return value; }
103     };
104   
105   /// typedef for true_type
106   typedef integral_constant<bool, true>     true_type;
107
108   /// typedef for false_type
109   typedef integral_constant<bool, false>    false_type;
110
111   template<typename _Tp, _Tp __v>
112     constexpr _Tp integral_constant<_Tp, __v>::value;
113
114   // primary type categories.
115
116   template<typename>
117     struct remove_cv;
118
119   template<typename>
120     struct __is_void_helper
121     : public false_type { };
122
123   template<>
124     struct __is_void_helper<void>
125     : public true_type { };
126
127   /// is_void
128   template<typename _Tp>
129     struct is_void
130     : public integral_constant<bool, (__is_void_helper<typename
131                                       remove_cv<_Tp>::type>::value)>
132     { };
133
134   template<typename>
135     struct __is_integral_helper
136     : public false_type { };
137
138   template<>
139     struct __is_integral_helper<bool>
140     : public true_type { };
141   
142   template<>
143     struct __is_integral_helper<char>
144     : public true_type { };
145
146   template<>
147     struct __is_integral_helper<signed char>
148     : public true_type { };
149
150   template<>
151     struct __is_integral_helper<unsigned char>
152     : public true_type { };
153
154 #ifdef _GLIBCXX_USE_WCHAR_T
155   template<>
156     struct __is_integral_helper<wchar_t>
157     : public true_type { };
158 #endif
159
160   template<>
161     struct __is_integral_helper<char16_t>
162     : public true_type { };
163
164   template<>
165     struct __is_integral_helper<char32_t>
166     : public true_type { };
167
168   template<>
169     struct __is_integral_helper<short>
170     : public true_type { };
171
172   template<>
173     struct __is_integral_helper<unsigned short>
174     : public true_type { };
175
176   template<>
177     struct __is_integral_helper<int>
178     : public true_type { };
179
180   template<>
181     struct __is_integral_helper<unsigned int>
182     : public true_type { };
183
184   template<>
185     struct __is_integral_helper<long>
186     : public true_type { };
187
188   template<>
189     struct __is_integral_helper<unsigned long>
190     : public true_type { };
191
192   template<>
193     struct __is_integral_helper<long long>
194     : public true_type { };
195
196   template<>
197     struct __is_integral_helper<unsigned long long>
198     : public true_type { };
199
200   /// is_integral
201   template<typename _Tp>
202     struct is_integral
203     : public integral_constant<bool, (__is_integral_helper<typename
204                                       remove_cv<_Tp>::type>::value)>
205     { };
206
207   template<typename>
208     struct __is_floating_point_helper
209     : public false_type { };
210
211   template<>
212     struct __is_floating_point_helper<float>
213     : public true_type { };
214
215   template<>
216     struct __is_floating_point_helper<double>
217     : public true_type { };
218
219   template<>
220     struct __is_floating_point_helper<long double>
221     : public true_type { };
222
223   /// is_floating_point
224   template<typename _Tp>
225     struct is_floating_point
226     : public integral_constant<bool, (__is_floating_point_helper<typename
227                                       remove_cv<_Tp>::type>::value)>
228     { };
229
230   /// is_array
231   template<typename>
232     struct is_array
233     : public false_type { };
234
235   template<typename _Tp, std::size_t _Size>
236     struct is_array<_Tp[_Size]>
237     : public true_type { };
238
239   template<typename _Tp>
240     struct is_array<_Tp[]>
241     : public true_type { };
242
243   template<typename>
244     struct __is_pointer_helper
245     : public false_type { };
246
247   template<typename _Tp>
248     struct __is_pointer_helper<_Tp*>
249     : public true_type { };
250
251   /// is_pointer
252   template<typename _Tp>
253     struct is_pointer
254     : public integral_constant<bool, (__is_pointer_helper<typename
255                                       remove_cv<_Tp>::type>::value)>
256     { };
257
258   /// is_lvalue_reference
259   template<typename>
260     struct is_lvalue_reference
261     : public false_type { };
262
263   template<typename _Tp>
264     struct is_lvalue_reference<_Tp&>
265     : public true_type { };
266
267   /// is_rvalue_reference
268   template<typename>
269     struct is_rvalue_reference
270     : public false_type { };
271
272   template<typename _Tp>
273     struct is_rvalue_reference<_Tp&&>
274     : public true_type { };
275
276   template<typename>
277     struct is_function;
278
279   template<typename>
280     struct __is_member_object_pointer_helper
281     : public false_type { };
282
283   template<typename _Tp, typename _Cp>
284     struct __is_member_object_pointer_helper<_Tp _Cp::*>
285     : public integral_constant<bool, !is_function<_Tp>::value> { };
286
287   /// is_member_object_pointer
288   template<typename _Tp>
289     struct is_member_object_pointer
290     : public integral_constant<bool, (__is_member_object_pointer_helper<
291                                       typename remove_cv<_Tp>::type>::value)>
292     { };
293
294   template<typename>
295     struct __is_member_function_pointer_helper
296     : public false_type { };
297
298   template<typename _Tp, typename _Cp>
299     struct __is_member_function_pointer_helper<_Tp _Cp::*>
300     : public integral_constant<bool, is_function<_Tp>::value> { };
301
302   /// is_member_function_pointer
303   template<typename _Tp>
304     struct is_member_function_pointer
305     : public integral_constant<bool, (__is_member_function_pointer_helper<
306                                       typename remove_cv<_Tp>::type>::value)>
307     { };
308
309   /// is_enum
310   template<typename _Tp>
311     struct is_enum
312     : public integral_constant<bool, __is_enum(_Tp)>
313     { };
314
315   /// is_union
316   template<typename _Tp>
317     struct is_union
318     : public integral_constant<bool, __is_union(_Tp)>
319     { };
320
321   /// is_class
322   template<typename _Tp>
323     struct is_class
324     : public integral_constant<bool, __is_class(_Tp)>
325     { };
326
327   /// is_function
328   template<typename>
329     struct is_function
330     : public false_type { };
331
332   template<typename _Res, typename... _ArgTypes>
333     struct is_function<_Res(_ArgTypes...)>
334     : public true_type { };
335
336   template<typename _Res, typename... _ArgTypes>
337     struct is_function<_Res(_ArgTypes......)>
338     : public true_type { };
339
340   template<typename _Res, typename... _ArgTypes>
341     struct is_function<_Res(_ArgTypes...) const>
342     : public true_type { };
343
344   template<typename _Res, typename... _ArgTypes>
345     struct is_function<_Res(_ArgTypes......) const>
346     : public true_type { };
347
348   template<typename _Res, typename... _ArgTypes>
349     struct is_function<_Res(_ArgTypes...) volatile>
350     : public true_type { };
351
352   template<typename _Res, typename... _ArgTypes>
353     struct is_function<_Res(_ArgTypes......) volatile>
354     : public true_type { };
355
356   template<typename _Res, typename... _ArgTypes>
357     struct is_function<_Res(_ArgTypes...) const volatile>
358     : public true_type { };
359
360   template<typename _Res, typename... _ArgTypes>
361     struct is_function<_Res(_ArgTypes......) const volatile>
362     : public true_type { };
363
364   template<typename>
365     struct __is_nullptr_t_helper
366     : public false_type { };
367
368   template<>
369     struct __is_nullptr_t_helper<std::nullptr_t>
370     : public true_type { };
371
372   // __is_nullptr_t (extension).
373   template<typename _Tp>
374     struct __is_nullptr_t
375     : public integral_constant<bool, (__is_nullptr_t_helper<typename
376                                       remove_cv<_Tp>::type>::value)>
377     { };
378
379   // composite type categories.
380
381   /// is_reference
382   template<typename _Tp>
383     struct is_reference
384     : public __or_<is_lvalue_reference<_Tp>,
385                    is_rvalue_reference<_Tp>>::type
386     { };
387
388   /// is_arithmetic
389   template<typename _Tp>
390     struct is_arithmetic
391     : public __or_<is_integral<_Tp>, is_floating_point<_Tp>>::type
392     { };
393
394   /// is_fundamental
395   template<typename _Tp>
396     struct is_fundamental
397     : public __or_<is_arithmetic<_Tp>, is_void<_Tp>>::type
398     { };
399
400   /// is_object
401   template<typename _Tp>
402     struct is_object
403     : public __not_<__or_<is_function<_Tp>, is_reference<_Tp>,
404                           is_void<_Tp>>>::type
405     { };
406
407   template<typename>
408     struct is_member_pointer;
409
410   /// is_scalar
411   template<typename _Tp>
412     struct is_scalar
413     : public __or_<is_arithmetic<_Tp>, is_enum<_Tp>, is_pointer<_Tp>,
414                    is_member_pointer<_Tp>, __is_nullptr_t<_Tp>>::type
415     { };
416
417   /// is_compound
418   template<typename _Tp>
419     struct is_compound
420     : public integral_constant<bool, !is_fundamental<_Tp>::value> { };
421
422   /// is_member_pointer
423   template<typename _Tp>
424     struct __is_member_pointer_helper
425     : public false_type { };
426
427   template<typename _Tp, typename _Cp>
428     struct __is_member_pointer_helper<_Tp _Cp::*>
429     : public true_type { };
430
431   template<typename _Tp>
432     struct is_member_pointer
433     : public integral_constant<bool, (__is_member_pointer_helper<
434                                       typename remove_cv<_Tp>::type>::value)>
435     { };
436
437   // type properties.
438
439   /// is_const
440   template<typename>
441     struct is_const
442     : public false_type { };
443
444   template<typename _Tp>
445     struct is_const<_Tp const>
446     : public true_type { };
447   
448   /// is_volatile
449   template<typename>
450     struct is_volatile
451     : public false_type { };
452
453   template<typename _Tp>
454     struct is_volatile<_Tp volatile>
455     : public true_type { };
456
457   /// is_trivial
458   template<typename _Tp>
459     struct is_trivial
460     : public integral_constant<bool, __is_trivial(_Tp)>
461     { };
462
463   /// is_trivially_copyable (still unimplemented)
464
465   /// is_standard_layout
466   template<typename _Tp>
467     struct is_standard_layout
468     : public integral_constant<bool, __is_standard_layout(_Tp)>
469     { };
470
471   /// is_pod
472   // Could use is_standard_layout && is_trivial instead of the builtin.
473   template<typename _Tp>
474     struct is_pod
475     : public integral_constant<bool, __is_pod(_Tp)>
476     { };
477
478   /// is_literal_type
479   template<typename _Tp>
480     struct is_literal_type
481     : public integral_constant<bool, __is_literal_type(_Tp)>
482     { };
483
484   /// is_empty
485   template<typename _Tp>
486     struct is_empty
487     : public integral_constant<bool, __is_empty(_Tp)>
488     { };
489
490   /// is_polymorphic
491   template<typename _Tp>
492     struct is_polymorphic
493     : public integral_constant<bool, __is_polymorphic(_Tp)>
494     { };
495
496   /// is_abstract
497   template<typename _Tp>
498     struct is_abstract
499     : public integral_constant<bool, __is_abstract(_Tp)>
500     { };
501
502   template<typename _Tp,
503            bool = is_integral<_Tp>::value,
504            bool = is_floating_point<_Tp>::value>
505     struct __is_signed_helper
506     : public false_type { };
507
508   template<typename _Tp>
509     struct __is_signed_helper<_Tp, false, true>
510     : public true_type { };
511
512   template<typename _Tp>
513     struct __is_signed_helper<_Tp, true, false>
514     : public integral_constant<bool, static_cast<bool>(_Tp(-1) < _Tp(0))>
515     { };
516
517   /// is_signed
518   template<typename _Tp>
519     struct is_signed
520     : public integral_constant<bool, __is_signed_helper<_Tp>::value>
521     { };
522
523   /// is_unsigned
524   template<typename _Tp>
525     struct is_unsigned
526     : public __and_<is_arithmetic<_Tp>, __not_<is_signed<_Tp>>>::type
527     { };
528
529
530   // destructible and constructible type properties
531
532   template<typename>
533     struct add_rvalue_reference;
534
535   template<typename _Tp>
536     typename add_rvalue_reference<_Tp>::type declval() noexcept;
537
538   template<typename, unsigned = 0>
539     struct extent;
540
541   template<typename>
542     struct remove_all_extents;
543
544   template<typename _Tp>
545     struct __is_array_known_bounds
546     : public integral_constant<bool, (extent<_Tp>::value > 0)>
547     { };
548
549   template<typename _Tp>
550     struct __is_array_unknown_bounds
551     : public __and_<is_array<_Tp>, __not_<extent<_Tp>>>::type
552     { };
553
554   struct __do_is_destructible_impl_1
555   {
556     template<typename _Up>
557       struct __w { _Up __u; };
558
559     template<typename _Tp, typename
560              = decltype(declval<__w<_Tp>&>().~__w<_Tp>())>
561       static true_type __test(int);
562
563     template<typename>
564       static false_type __test(...);
565   };
566
567   template<typename _Tp>
568     struct __is_destructible_impl_1
569     : public __do_is_destructible_impl_1
570     {
571       typedef decltype(__test<_Tp>(0)) type;
572     };
573
574   struct __do_is_destructible_impl_2
575   {
576     template<typename _Tp, typename = decltype(declval<_Tp&>().~_Tp())>
577       static true_type __test(int);
578
579     template<typename>
580       static false_type __test(...);
581   };
582
583   template<typename _Tp>
584     struct __is_destructible_impl_2
585     : public __do_is_destructible_impl_2
586     {
587       typedef decltype(__test<_Tp>(0)) type;
588     };
589
590   template<typename _Tp,
591            bool = __or_<is_void<_Tp>,
592                         __is_array_unknown_bounds<_Tp>>::value,
593            bool = __or_<is_reference<_Tp>, is_function<_Tp>>::value>
594     struct __is_destructible_safe;
595
596   template<typename _Tp>
597     struct __is_destructible_safe<_Tp, false, false>
598     : public conditional<is_abstract<_Tp>::value,
599                          __is_destructible_impl_2<_Tp>,
600                          __is_destructible_impl_1<_Tp>>::type::type
601     { };
602
603   template<typename _Tp>
604     struct __is_destructible_safe<_Tp, true, false>
605     : public false_type { };
606
607   template<typename _Tp>
608     struct __is_destructible_safe<_Tp, false, true>
609     : public true_type { };
610
611   /// is_destructible
612   template<typename _Tp>
613     struct is_destructible
614     : public integral_constant<bool, (__is_destructible_safe<_Tp>::value)>
615     { };
616
617   struct __do_is_default_constructible_impl
618   {
619     template<typename _Tp, typename = decltype(_Tp())>
620       static true_type __test(int);
621
622     template<typename>
623       static false_type __test(...);
624   };
625
626   template<typename _Tp>
627     struct __is_default_constructible_impl
628     : public __do_is_default_constructible_impl
629     {
630       typedef decltype(__test<_Tp>(0)) type;
631     };
632
633   template<typename _Tp>
634     struct __is_default_constructible_atom
635     : public __and_<is_destructible<_Tp>,
636                     __is_default_constructible_impl<_Tp>>::type::type
637     { };
638
639   template<typename _Tp, bool = is_array<_Tp>::value>
640     struct __is_default_constructible_safe;
641
642   // The following technique is a workaround for a gcc defect, which does
643   // not sfinae away attempts to default-construct arrays of unknown bounds.
644   // Complete arrays can be default-constructed, if the element type is
645   // default-constructible, but arrays with unknown bounds are not:
646
647   template<typename _Tp>
648     struct __is_default_constructible_safe<_Tp, true>
649     : public __and_<__is_array_known_bounds<_Tp>,
650                     __is_default_constructible_atom<typename
651                       remove_all_extents<_Tp>::type>>::type::type
652     { };
653
654   template<typename _Tp>
655     struct __is_default_constructible_safe<_Tp, false>
656     : public __is_default_constructible_atom<_Tp>::type
657     { };
658
659   /// is_default_constructible
660   template<typename _Tp>
661     struct is_default_constructible
662     : public integral_constant<bool, (__is_default_constructible_safe<
663                                       _Tp>::value)>
664     { };
665
666   struct __do_is_static_castable_impl
667   {
668     template<typename _From, typename _To, typename
669              = decltype(static_cast<_To>(declval<_From>()))>
670       static true_type __test(int);
671
672     template<typename, typename>
673       static false_type __test(...);
674   };
675
676   template<typename _From, typename _To>
677     struct __is_static_castable_impl
678     : public __do_is_static_castable_impl
679     {
680       typedef decltype(__test<_From, _To>(0)) type;
681     };
682
683   template<typename _From, typename _To>
684     struct __is_static_castable_safe
685     : public __and_<__or_<is_void<_To>, is_destructible<_To>>,
686                     __is_static_castable_impl<_From, _To>>::type::type
687     { };
688
689   // __is_static_castable
690   template<typename _From, typename _To>
691     struct __is_static_castable
692     : public integral_constant<bool, (__is_static_castable_safe<
693                                       _From, _To>::value)>
694     { };
695
696   struct __do_is_direct_constructible_impl
697   {
698     template<typename _Tp, typename _Arg, typename
699              = decltype(::new _Tp(declval<_Arg>()))>
700       static true_type __test(int);
701
702     template<typename, typename>
703       static false_type __test(...);
704   };
705
706   template<typename _Tp, typename _Arg>
707     struct __is_direct_constructible_impl
708     : public __do_is_direct_constructible_impl
709     {
710       typedef decltype(__test<_Tp, _Arg>(0)) type;
711     };
712
713   template<typename _Tp, typename _Arg>
714     struct __is_direct_constructible_new_safe
715     : public __and_<is_destructible<_Tp>,
716                     __is_direct_constructible_impl<_Tp, _Arg>>::type::type
717     { };
718
719   template<typename, typename>
720     struct is_same;
721
722   template<typename, typename>
723     struct is_base_of;
724
725   template<typename>
726     struct remove_reference;
727
728   template<typename _From, typename _To, bool
729            = is_reference<_From>::value>
730     struct __is_base_to_derived_ref;
731
732   template<typename _From, typename _To>
733     struct __is_base_to_derived_ref<_From, _To, true>
734     {
735       typedef typename remove_cv<typename remove_reference<_From
736         >::type>::type __src_t;
737       typedef typename remove_cv<typename remove_reference<_To
738         >::type>::type __dst_t;
739       typedef typename __and_<
740         __not_<is_same<__src_t, __dst_t>>,
741         is_base_of<__src_t, __dst_t>
742       >::type type;
743       static constexpr bool value = type::value;
744     };
745
746   template<typename _From, typename _To>
747     struct __is_base_to_derived_ref<_From, _To, false>
748     : public false_type
749     { };
750
751   template<typename _From, typename _To, bool
752            = __and_<is_lvalue_reference<_From>,
753                     is_rvalue_reference<_To>>::value>
754     struct __is_lvalue_to_rvalue_ref;
755
756   template<typename _From, typename _To>
757     struct __is_lvalue_to_rvalue_ref<_From, _To, true>
758     {
759       typedef typename remove_cv<typename remove_reference<
760         _From>::type>::type __src_t;
761       typedef typename remove_cv<typename remove_reference<
762         _To>::type>::type __dst_t;
763       typedef typename __or_<
764         is_same<__src_t, __dst_t>,
765         is_base_of<__dst_t, __src_t>
766       >::type type;
767       static constexpr bool value = type::value;
768     };
769
770   template<typename _From, typename _To>
771     struct __is_lvalue_to_rvalue_ref<_From, _To, false>
772     : public false_type
773     { };
774
775   // Here we handle direct-initialization to a reference type
776   // as equivalent to a static_cast modulo overshooting conversions.
777   // These are restricted to the following conversion:
778   //    a) A base class to a derived class reference
779   //    b) An lvalue-reference to an rvalue-reference
780
781   template<typename _Tp, typename _Arg>
782     struct __is_direct_constructible_ref_cast
783     : public __and_<__is_static_castable<_Arg, _Tp>,
784                     __not_<__or_<__is_base_to_derived_ref<_Arg, _Tp>,
785                                  __is_lvalue_to_rvalue_ref<_Arg, _Tp>
786                    >>>::type::type
787     { };
788
789   // Direct-initialization is tricky, because of functional
790   // casts: For a conversion to reference we fall back to a
791   // static_cast modulo extra cases, otherwise we use a
792   // new expression:
793
794   template<typename _Tp, typename _Arg>
795     struct __is_direct_constructible_new
796     : public conditional<is_reference<_Tp>::value,
797                          __is_direct_constructible_ref_cast<_Tp, _Arg>,
798                          __is_direct_constructible_new_safe<_Tp, _Arg>
799                          >::type
800     { };
801
802   template<typename _Tp, typename _Arg>
803     struct __is_direct_constructible
804     : public integral_constant<bool, (__is_direct_constructible_new<
805                                       _Tp, _Arg>::type::value)>
806     { };
807
808   struct __do_is_nary_constructible_impl
809   {
810     template<typename _Tp, typename... _Args, typename
811              = decltype(_Tp(declval<_Args>()...))>
812       static true_type __test(int);
813
814     template<typename, typename...>
815       static false_type __test(...);
816   };
817
818   template<typename _Tp, typename... _Args>
819     struct __is_nary_constructible_impl
820     : public __do_is_nary_constructible_impl
821     {
822       typedef decltype(__test<_Tp, _Args...>(0)) type;
823     };
824
825   template<typename _Tp, typename... _Args>
826     struct __is_nary_constructible
827     : public __and_<is_destructible<_Tp>,
828                     __is_nary_constructible_impl<_Tp, _Args...>
829                    >::type::type
830     {
831       static_assert(sizeof...(_Args) > 1,
832                     "Only useful for > 1 arguments");
833     };
834
835   template<typename _Tp, typename... _Args>
836     struct __is_constructible_impl
837     : public __is_nary_constructible<_Tp, _Args...>
838     { };
839
840   template<typename _Tp, typename _Arg>
841     struct __is_constructible_impl<_Tp, _Arg>
842     : public __is_direct_constructible<_Tp, _Arg>
843     { };
844
845   template<typename _Tp>
846     struct __is_constructible_impl<_Tp>
847     : public is_default_constructible<_Tp>
848     { };
849
850   /// is_constructible
851   template<typename _Tp, typename... _Args>
852     struct is_constructible
853     : public integral_constant<bool, (__is_constructible_impl<_Tp,
854                                       _Args...>::value)>
855     { };
856
857
858   template<bool, typename _Tp, typename... _Args>
859     struct __is_nt_constructible_helper
860     { static const bool __value = false; };
861
862   template<typename _Tp, typename... _Args>
863     struct __is_nt_constructible_helper<true, _Tp, _Args...>
864     { static const bool __value = noexcept(_Tp(declval<_Args>()...)); };
865
866   template<typename _Tp, typename _Arg>
867     struct __is_nt_constructible_helper<true, _Tp, _Arg>
868     {
869       static const bool __value = noexcept(static_cast<_Tp>(declval<_Arg>()));
870     };
871
872   /// is_nothrow_constructible
873   template<typename _Tp, typename... _Args>
874     struct is_nothrow_constructible
875     : public integral_constant<bool,
876           __is_nt_constructible_helper<is_constructible<_Tp, _Args...>::value,
877                                        _Tp, _Args...>::__value>
878     { };
879
880   /// has_trivial_default_constructor
881   template<typename _Tp>
882     struct has_trivial_default_constructor
883     : public integral_constant<bool, __has_trivial_constructor(_Tp)>
884     { };
885
886   /// has_trivial_copy_constructor
887   template<typename _Tp>
888     struct has_trivial_copy_constructor
889     : public integral_constant<bool, __has_trivial_copy(_Tp)>
890     { };
891
892   /// has_trivial_copy_assign
893   template<typename _Tp>
894     struct has_trivial_copy_assign
895     : public integral_constant<bool, __has_trivial_assign(_Tp)>
896     { };
897
898   /// has_trivial_destructor
899   template<typename _Tp>
900     struct has_trivial_destructor
901     : public integral_constant<bool, __has_trivial_destructor(_Tp)>
902     { };
903
904   /// has_nothrow_default_constructor
905   template<typename _Tp>
906     struct has_nothrow_default_constructor
907     : public integral_constant<bool, __has_nothrow_constructor(_Tp)>
908     { };
909
910   /// has_nothrow_copy_constructor
911   template<typename _Tp>
912     struct has_nothrow_copy_constructor
913     : public integral_constant<bool, __has_nothrow_copy(_Tp)>
914     { };
915
916   /// has_nothrow_copy_assign
917   template<typename _Tp>
918     struct has_nothrow_copy_assign
919     : public integral_constant<bool, __has_nothrow_assign(_Tp)>
920     { };
921
922   /// has_virtual_destructor
923   template<typename _Tp>
924     struct has_virtual_destructor
925     : public integral_constant<bool, __has_virtual_destructor(_Tp)>
926     { };
927
928   
929   // type property queries.
930
931   /// alignment_of
932   template<typename _Tp>
933     struct alignment_of
934     : public integral_constant<std::size_t, __alignof__(_Tp)> { };
935   
936   /// rank
937   template<typename>
938     struct rank
939     : public integral_constant<std::size_t, 0> { };
940    
941   template<typename _Tp, std::size_t _Size>
942     struct rank<_Tp[_Size]>
943     : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { };
944
945   template<typename _Tp>
946     struct rank<_Tp[]>
947     : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { };
948
949   /// extent
950   template<typename, unsigned _Uint>
951     struct extent
952     : public integral_constant<std::size_t, 0> { };
953   
954   template<typename _Tp, unsigned _Uint, std::size_t _Size>
955     struct extent<_Tp[_Size], _Uint>
956     : public integral_constant<std::size_t,
957                                _Uint == 0 ? _Size : extent<_Tp,
958                                                            _Uint - 1>::value>
959     { };
960
961   template<typename _Tp, unsigned _Uint>
962     struct extent<_Tp[], _Uint>
963     : public integral_constant<std::size_t,
964                                _Uint == 0 ? 0 : extent<_Tp,
965                                                        _Uint - 1>::value>
966     { };
967
968
969   // type relations.
970
971   /// is_same
972   template<typename, typename>
973     struct is_same
974     : public false_type { };
975
976   template<typename _Tp>
977     struct is_same<_Tp, _Tp>
978     : public true_type { };
979
980   /// is_base_of
981   template<typename _Base, typename _Derived>
982     struct is_base_of
983     : public integral_constant<bool, __is_base_of(_Base, _Derived)>
984     { };
985
986   template<typename _From, typename _To,
987            bool = __or_<is_void<_From>, is_function<_To>,
988                         is_array<_To>>::value>
989     struct __is_convertible_helper
990     { static const bool __value = is_void<_To>::value; };
991
992   template<typename _From, typename _To>
993     class __is_convertible_helper<_From, _To, false>
994     : public __sfinae_types
995     {
996       template<typename _To1>
997         static void __test_aux(_To1);
998
999       template<typename _From1, typename _To1>
1000         static decltype(__test_aux<_To1>(std::declval<_From1>()), __one())
1001         __test(int);
1002
1003       template<typename, typename>
1004         static __two __test(...);
1005
1006     public:
1007       static const bool __value = sizeof(__test<_From, _To>(0)) == 1;
1008     };
1009
1010   /// is_convertible
1011   template<typename _From, typename _To>
1012     struct is_convertible
1013     : public integral_constant<bool,
1014                                __is_convertible_helper<_From, _To>::__value>
1015     { };
1016
1017   /// is_explicitly_convertible
1018   template<typename _From, typename _To>
1019     struct is_explicitly_convertible
1020     : public is_constructible<_To, _From>
1021     { };
1022
1023
1024   // const-volatile modifications.
1025
1026   /// remove_const
1027   template<typename _Tp>
1028     struct remove_const
1029     { typedef _Tp     type; };
1030
1031   template<typename _Tp>
1032     struct remove_const<_Tp const>
1033     { typedef _Tp     type; };
1034   
1035   /// remove_volatile
1036   template<typename _Tp>
1037     struct remove_volatile
1038     { typedef _Tp     type; };
1039
1040   template<typename _Tp>
1041     struct remove_volatile<_Tp volatile>
1042     { typedef _Tp     type; };
1043   
1044   /// remove_cv
1045   template<typename _Tp>
1046     struct remove_cv
1047     {
1048       typedef typename
1049       remove_const<typename remove_volatile<_Tp>::type>::type     type;
1050     };
1051   
1052   /// add_const
1053   template<typename _Tp>
1054     struct add_const
1055     { typedef _Tp const     type; };
1056    
1057   /// add_volatile
1058   template<typename _Tp>
1059     struct add_volatile
1060     { typedef _Tp volatile     type; };
1061   
1062   /// add_cv
1063   template<typename _Tp>
1064     struct add_cv
1065     {
1066       typedef typename
1067       add_const<typename add_volatile<_Tp>::type>::type     type;
1068     };
1069
1070
1071   // Reference transformations.
1072
1073   /// remove_reference
1074   template<typename _Tp>
1075     struct remove_reference
1076     { typedef _Tp   type; };
1077
1078   template<typename _Tp>
1079     struct remove_reference<_Tp&>
1080     { typedef _Tp   type; };
1081
1082   template<typename _Tp>
1083     struct remove_reference<_Tp&&>
1084     { typedef _Tp   type; };
1085
1086   template<typename _Tp,
1087            bool = __and_<__not_<is_reference<_Tp>>,
1088                          __not_<is_void<_Tp>>>::value,
1089            bool = is_rvalue_reference<_Tp>::value>
1090     struct __add_lvalue_reference_helper
1091     { typedef _Tp   type; };
1092
1093   template<typename _Tp>
1094     struct __add_lvalue_reference_helper<_Tp, true, false>
1095     { typedef _Tp&   type; };
1096
1097   template<typename _Tp>
1098     struct __add_lvalue_reference_helper<_Tp, false, true>
1099     { typedef typename remove_reference<_Tp>::type&   type; };
1100
1101   /// add_lvalue_reference
1102   template<typename _Tp>
1103     struct add_lvalue_reference
1104     : public __add_lvalue_reference_helper<_Tp>
1105     { };
1106
1107   template<typename _Tp,
1108            bool = __and_<__not_<is_reference<_Tp>>,
1109                          __not_<is_void<_Tp>>>::value>
1110     struct __add_rvalue_reference_helper
1111     { typedef _Tp   type; };
1112
1113   template<typename _Tp>
1114     struct __add_rvalue_reference_helper<_Tp, true>
1115     { typedef _Tp&&   type; };
1116
1117   /// add_rvalue_reference
1118   template<typename _Tp>
1119     struct add_rvalue_reference
1120     : public __add_rvalue_reference_helper<_Tp>
1121     { };
1122
1123
1124   // sign modifications.
1125
1126   // Utility for constructing identically cv-qualified types.
1127   template<typename _Unqualified, bool _IsConst, bool _IsVol>
1128     struct __cv_selector;
1129
1130   template<typename _Unqualified>
1131     struct __cv_selector<_Unqualified, false, false>
1132     { typedef _Unqualified __type; };
1133
1134   template<typename _Unqualified>
1135     struct __cv_selector<_Unqualified, false, true>
1136     { typedef volatile _Unqualified __type; };
1137
1138   template<typename _Unqualified>
1139     struct __cv_selector<_Unqualified, true, false>
1140     { typedef const _Unqualified __type; };
1141
1142   template<typename _Unqualified>
1143     struct __cv_selector<_Unqualified, true, true>
1144     { typedef const volatile _Unqualified __type; };
1145
1146   template<typename _Qualified, typename _Unqualified,
1147            bool _IsConst = is_const<_Qualified>::value,
1148            bool _IsVol = is_volatile<_Qualified>::value>
1149     class __match_cv_qualifiers
1150     {
1151       typedef __cv_selector<_Unqualified, _IsConst, _IsVol> __match;
1152
1153     public:
1154       typedef typename __match::__type __type; 
1155     };
1156
1157   // Utility for finding the unsigned versions of signed integral types.
1158   template<typename _Tp>
1159     struct __make_unsigned
1160     { typedef _Tp __type; };
1161
1162   template<>
1163     struct __make_unsigned<char>
1164     { typedef unsigned char __type; };
1165
1166   template<>
1167     struct __make_unsigned<signed char>
1168     { typedef unsigned char __type; };
1169
1170   template<>
1171     struct __make_unsigned<short>
1172     { typedef unsigned short __type; };
1173
1174   template<>
1175     struct __make_unsigned<int>
1176     { typedef unsigned int __type; };
1177
1178   template<>
1179     struct __make_unsigned<long>
1180     { typedef unsigned long __type; };
1181
1182   template<>
1183     struct __make_unsigned<long long>
1184     { typedef unsigned long long __type; };
1185
1186   // Select between integral and enum: not possible to be both.
1187   template<typename _Tp, 
1188            bool _IsInt = is_integral<_Tp>::value,
1189            bool _IsEnum = is_enum<_Tp>::value>
1190     class __make_unsigned_selector;
1191
1192   template<typename _Tp>
1193     class __make_unsigned_selector<_Tp, true, false>
1194     {
1195       typedef __make_unsigned<typename remove_cv<_Tp>::type> __unsignedt;
1196       typedef typename __unsignedt::__type __unsigned_type;
1197       typedef __match_cv_qualifiers<_Tp, __unsigned_type> __cv_unsigned;
1198
1199     public:
1200       typedef typename __cv_unsigned::__type __type;
1201     };
1202
1203   template<typename _Tp>
1204     class __make_unsigned_selector<_Tp, false, true>
1205     {
1206       // With -fshort-enums, an enum may be as small as a char.
1207       typedef unsigned char __smallest;
1208       static const bool __b0 = sizeof(_Tp) <= sizeof(__smallest);
1209       static const bool __b1 = sizeof(_Tp) <= sizeof(unsigned short);
1210       static const bool __b2 = sizeof(_Tp) <= sizeof(unsigned int);
1211       typedef conditional<__b2, unsigned int, unsigned long> __cond2;
1212       typedef typename __cond2::type __cond2_type;
1213       typedef conditional<__b1, unsigned short, __cond2_type> __cond1;
1214       typedef typename __cond1::type __cond1_type;
1215
1216     public:
1217       typedef typename conditional<__b0, __smallest, __cond1_type>::type __type;
1218     };
1219
1220   // Given an integral/enum type, return the corresponding unsigned
1221   // integer type.
1222   // Primary template.
1223   /// make_unsigned
1224   template<typename _Tp>
1225     struct make_unsigned 
1226     { typedef typename __make_unsigned_selector<_Tp>::__type type; };
1227
1228   // Integral, but don't define.
1229   template<>
1230     struct make_unsigned<bool>;
1231
1232
1233   // Utility for finding the signed versions of unsigned integral types.
1234   template<typename _Tp>
1235     struct __make_signed
1236     { typedef _Tp __type; };
1237
1238   template<>
1239     struct __make_signed<char>
1240     { typedef signed char __type; };
1241
1242   template<>
1243     struct __make_signed<unsigned char>
1244     { typedef signed char __type; };
1245
1246   template<>
1247     struct __make_signed<unsigned short>
1248     { typedef signed short __type; };
1249
1250   template<>
1251     struct __make_signed<unsigned int>
1252     { typedef signed int __type; };
1253
1254   template<>
1255     struct __make_signed<unsigned long>
1256     { typedef signed long __type; };
1257
1258   template<>
1259     struct __make_signed<unsigned long long>
1260     { typedef signed long long __type; };
1261
1262   // Select between integral and enum: not possible to be both.
1263   template<typename _Tp, 
1264            bool _IsInt = is_integral<_Tp>::value,
1265            bool _IsEnum = is_enum<_Tp>::value>
1266     class __make_signed_selector;
1267
1268   template<typename _Tp>
1269     class __make_signed_selector<_Tp, true, false>
1270     {
1271       typedef __make_signed<typename remove_cv<_Tp>::type> __signedt;
1272       typedef typename __signedt::__type __signed_type;
1273       typedef __match_cv_qualifiers<_Tp, __signed_type> __cv_signed;
1274
1275     public:
1276       typedef typename __cv_signed::__type __type;
1277     };
1278
1279   template<typename _Tp>
1280     class __make_signed_selector<_Tp, false, true>
1281     {
1282       // With -fshort-enums, an enum may be as small as a char.
1283       typedef signed char __smallest;
1284       static const bool __b0 = sizeof(_Tp) <= sizeof(__smallest);
1285       static const bool __b1 = sizeof(_Tp) <= sizeof(signed short);
1286       static const bool __b2 = sizeof(_Tp) <= sizeof(signed int);
1287       typedef conditional<__b2, signed int, signed long> __cond2;
1288       typedef typename __cond2::type __cond2_type;
1289       typedef conditional<__b1, signed short, __cond2_type> __cond1;
1290       typedef typename __cond1::type __cond1_type;
1291
1292     public:
1293       typedef typename conditional<__b0, __smallest, __cond1_type>::type __type;
1294     };
1295
1296   // Given an integral/enum type, return the corresponding signed
1297   // integer type.
1298   // Primary template.
1299   /// make_signed
1300   template<typename _Tp>
1301     struct make_signed 
1302     { typedef typename __make_signed_selector<_Tp>::__type type; };
1303
1304   // Integral, but don't define.
1305   template<>
1306     struct make_signed<bool>;
1307
1308
1309   // array modifications.
1310
1311   /// remove_extent
1312   template<typename _Tp>
1313     struct remove_extent
1314     { typedef _Tp     type; };
1315
1316   template<typename _Tp, std::size_t _Size>
1317     struct remove_extent<_Tp[_Size]>
1318     { typedef _Tp     type; };
1319
1320   template<typename _Tp>
1321     struct remove_extent<_Tp[]>
1322     { typedef _Tp     type; };
1323
1324   /// remove_all_extents
1325   template<typename _Tp>
1326     struct remove_all_extents
1327     { typedef _Tp     type; };
1328
1329   template<typename _Tp, std::size_t _Size>
1330     struct remove_all_extents<_Tp[_Size]>
1331     { typedef typename remove_all_extents<_Tp>::type     type; };
1332
1333   template<typename _Tp>
1334     struct remove_all_extents<_Tp[]>
1335     { typedef typename remove_all_extents<_Tp>::type     type; };
1336
1337
1338   // pointer modifications.
1339
1340   template<typename _Tp, typename>
1341     struct __remove_pointer_helper
1342     { typedef _Tp     type; };
1343
1344   template<typename _Tp, typename _Up>
1345     struct __remove_pointer_helper<_Tp, _Up*>
1346     { typedef _Up     type; };
1347
1348   /// remove_pointer
1349   template<typename _Tp>
1350     struct remove_pointer
1351     : public __remove_pointer_helper<_Tp, typename remove_cv<_Tp>::type>
1352     { };
1353
1354   /// add_pointer
1355   template<typename _Tp>
1356     struct add_pointer
1357     { typedef typename remove_reference<_Tp>::type*     type; };
1358
1359
1360   template<std::size_t _Len>
1361     struct __aligned_storage_msa
1362     { 
1363       union __type
1364       {
1365         unsigned char __data[_Len];
1366         struct __attribute__((__aligned__)) { } __align; 
1367       };
1368     };
1369
1370   /**
1371    *  @brief Alignment type.
1372    *
1373    *  The value of _Align is a default-alignment which shall be the
1374    *  most stringent alignment requirement for any C++ object type
1375    *  whose size is no greater than _Len (3.9). The member typedef
1376    *  type shall be a POD type suitable for use as uninitialized
1377    *  storage for any object whose size is at most _Len and whose
1378    *  alignment is a divisor of _Align.
1379   */
1380   template<std::size_t _Len, std::size_t _Align =
1381            __alignof__(typename __aligned_storage_msa<_Len>::__type)>
1382     struct aligned_storage
1383     { 
1384       union type
1385       {
1386         unsigned char __data[_Len];
1387         struct __attribute__((__aligned__((_Align)))) { } __align; 
1388       };
1389     };
1390
1391
1392   // Decay trait for arrays and functions, used for perfect forwarding
1393   // in make_pair, make_tuple, etc.
1394   template<typename _Up, 
1395            bool _IsArray = is_array<_Up>::value,
1396            bool _IsFunction = is_function<_Up>::value> 
1397     struct __decay_selector;
1398
1399   // NB: DR 705.
1400   template<typename _Up> 
1401     struct __decay_selector<_Up, false, false>
1402     { typedef typename remove_cv<_Up>::type __type; };
1403
1404   template<typename _Up> 
1405     struct __decay_selector<_Up, true, false>
1406     { typedef typename remove_extent<_Up>::type* __type; };
1407
1408   template<typename _Up> 
1409     struct __decay_selector<_Up, false, true>
1410     { typedef typename add_pointer<_Up>::type __type; };
1411
1412   /// decay
1413   template<typename _Tp> 
1414     class decay 
1415     { 
1416       typedef typename remove_reference<_Tp>::type __remove_type;
1417
1418     public:
1419       typedef typename __decay_selector<__remove_type>::__type type;
1420     };
1421
1422   template<typename _Tp>
1423     class reference_wrapper;
1424
1425   // Helper which adds a reference to a type when given a reference_wrapper
1426   template<typename _Tp>
1427     struct __strip_reference_wrapper
1428     {
1429       typedef _Tp __type;
1430     };
1431
1432   template<typename _Tp>
1433     struct __strip_reference_wrapper<reference_wrapper<_Tp> >
1434     {
1435       typedef _Tp& __type;
1436     };
1437
1438   template<typename _Tp>
1439     struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
1440     {
1441       typedef _Tp& __type;
1442     };
1443
1444   template<typename _Tp>
1445     struct __decay_and_strip
1446     {
1447       typedef typename __strip_reference_wrapper<
1448         typename decay<_Tp>::type>::__type __type;
1449     };
1450
1451
1452   // Define a nested type if some predicate holds.
1453   // Primary template.
1454   /// enable_if
1455   template<bool, typename _Tp = void>
1456     struct enable_if 
1457     { };
1458
1459   // Partial specialization for true.
1460   template<typename _Tp>
1461     struct enable_if<true, _Tp>
1462     { typedef _Tp type; };
1463
1464
1465   // A conditional expression, but for types. If true, first, if false, second.
1466   // Primary template.
1467   /// conditional
1468   template<bool _Cond, typename _Iftrue, typename _Iffalse>
1469     struct conditional
1470     { typedef _Iftrue type; };
1471
1472   // Partial specialization for false.
1473   template<typename _Iftrue, typename _Iffalse>
1474     struct conditional<false, _Iftrue, _Iffalse>
1475     { typedef _Iffalse type; };
1476
1477
1478   /// common_type
1479   template<typename... _Tp>
1480     struct common_type;
1481
1482   template<typename _Tp>
1483     struct common_type<_Tp>
1484     { typedef _Tp type; };
1485
1486   template<typename _Tp, typename _Up>
1487     struct common_type<_Tp, _Up>
1488     { typedef decltype(true ? declval<_Tp>() : declval<_Up>()) type; };
1489
1490   template<typename _Tp, typename _Up, typename... _Vp>
1491     struct common_type<_Tp, _Up, _Vp...>
1492     {
1493       typedef typename
1494         common_type<typename common_type<_Tp, _Up>::type, _Vp...>::type type;
1495     };
1496
1497   /// underlying_type (still unimplemented)
1498
1499   /// result_of
1500   template<typename _Signature>
1501     class result_of;
1502
1503   template<typename _Functor, typename... _ArgTypes>
1504     struct result_of<_Functor(_ArgTypes...)>
1505     {
1506       typedef
1507         decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )
1508         type;
1509     };
1510
1511   /// declval
1512   template<typename _Tp>
1513     struct __declval_protector
1514     {
1515       static const bool __stop = false;
1516       static typename add_rvalue_reference<_Tp>::type __delegate();
1517     };
1518
1519   template<typename _Tp>
1520     inline typename add_rvalue_reference<_Tp>::type
1521     declval() noexcept
1522     {
1523       static_assert(__declval_protector<_Tp>::__stop,
1524                     "declval() must not be used!");
1525       return __declval_protector<_Tp>::__delegate();
1526     }
1527
1528   /**
1529    *  Use SFINAE to determine if the type _Tp has a publicly-accessible
1530    *  member type _NTYPE.
1531    */
1532 #define _GLIBCXX_HAS_NESTED_TYPE(_NTYPE)                         \
1533   template<typename _Tp>                                         \
1534     class __has_##_NTYPE##_helper                                \
1535     : __sfinae_types                                             \
1536     {                                                            \
1537       template<typename _Up>                                     \
1538         struct _Wrap_type                                        \
1539         { };                                                     \
1540                                                                  \
1541       template<typename _Up>                                     \
1542         static __one __test(_Wrap_type<typename _Up::_NTYPE>*);  \
1543                                                                  \
1544       template<typename _Up>                                     \
1545         static __two __test(...);                                \
1546                                                                  \
1547     public:                                                      \
1548       static const bool value = sizeof(__test<_Tp>(0)) == 1;     \
1549     };                                                           \
1550                                                                  \
1551   template<typename _Tp>                                         \
1552     struct __has_##_NTYPE                                        \
1553     : integral_constant<bool, __has_##_NTYPE##_helper            \
1554                         <typename remove_cv<_Tp>::type>::value>  \
1555     { };
1556
1557   // @} group metaprogramming
1558 _GLIBCXX_END_NAMESPACE_VERSION
1559 } // namespace
1560
1561 #endif  // __GXX_EXPERIMENTAL_CXX0X__
1562
1563 #endif  // _GLIBCXX_TYPE_TRAITS