OSDN Git Service

4533ee5dc6cafca8cc7ada99dba6b547017753a1
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / tr1 / type_traits
1 // TR1 type_traits -*- C++ -*-
2
3 // Copyright (C) 2004, 2005, 2006, 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file tr1/type_traits
31  *  This is a TR1 C++ Library header. 
32  */
33
34 #ifndef _GLIBCXX_TR1_TYPE_TRAITS
35 #define _GLIBCXX_TR1_TYPE_TRAITS 1
36
37 #pragma GCC system_header
38
39 #if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
40 #  error TR1 header cannot be included from C++0x header
41 #endif
42
43 #include <cstddef>
44
45 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
46 #  include <tr1_impl/type_traits>
47 #else
48 #  define _GLIBCXX_INCLUDE_AS_TR1
49 #  define _GLIBCXX_BEGIN_NAMESPACE_TR1 namespace tr1 {
50 #  define _GLIBCXX_END_NAMESPACE_TR1 }
51 #  define _GLIBCXX_TR1 tr1::
52 #  include <tr1_impl/type_traits>
53 #  undef _GLIBCXX_TR1
54 #  undef _GLIBCXX_END_NAMESPACE_TR1
55 #  undef _GLIBCXX_BEGIN_NAMESPACE_TR1
56 #  undef _GLIBCXX_INCLUDE_AS_TR1
57 #endif
58
59 namespace std
60 {
61 namespace tr1
62 {
63 #define _DEFINE_SPEC_HELPER(_Spec)                 \
64   template<>                                       \
65     struct _Spec                                   \
66     : public true_type { };
67
68 #define _DEFINE_SPEC(_Trait, _Type)                \
69   _DEFINE_SPEC_HELPER(_Trait<_Type>)               \
70   _DEFINE_SPEC_HELPER(_Trait<_Type const>)         \
71   _DEFINE_SPEC_HELPER(_Trait<_Type volatile>)      \
72   _DEFINE_SPEC_HELPER(_Trait<_Type const volatile>)
73
74   template<typename>
75     struct is_reference
76     : public false_type { };
77
78   template<typename _Tp>
79     struct is_reference<_Tp&>
80     : public true_type { };
81
82   template<typename _Tp>
83     struct is_pod
84     : public integral_constant<bool, __is_pod(_Tp) || is_void<_Tp>::value>
85     { };
86
87   template<typename _Tp>
88     struct has_trivial_constructor
89     : public integral_constant<bool, is_pod<_Tp>::value>
90     { };
91
92   template<typename _Tp>
93     struct has_trivial_copy
94     : public integral_constant<bool, is_pod<_Tp>::value>
95     { };
96
97   template<typename _Tp>
98     struct has_trivial_assign
99     : public integral_constant<bool, is_pod<_Tp>::value>
100     { };
101
102   template<typename _Tp>
103     struct has_trivial_destructor
104     : public integral_constant<bool, is_pod<_Tp>::value>
105     { };
106
107   template<typename _Tp>
108     struct has_nothrow_constructor
109     : public integral_constant<bool, is_pod<_Tp>::value>
110     { };
111
112   template<typename _Tp>
113     struct has_nothrow_copy
114     : public integral_constant<bool, is_pod<_Tp>::value>
115     { };
116
117   template<typename _Tp>
118     struct has_nothrow_assign
119     : public integral_constant<bool, is_pod<_Tp>::value>
120     { };
121
122   template<typename>
123     struct is_signed
124     : public false_type { };
125   _DEFINE_SPEC(is_signed, signed char)
126   _DEFINE_SPEC(is_signed, short)
127   _DEFINE_SPEC(is_signed, int)
128   _DEFINE_SPEC(is_signed, long)
129   _DEFINE_SPEC(is_signed, long long)
130
131   template<typename>
132     struct is_unsigned
133     : public false_type { };
134   _DEFINE_SPEC(is_unsigned, unsigned char)
135   _DEFINE_SPEC(is_unsigned, unsigned short)
136   _DEFINE_SPEC(is_unsigned, unsigned int)
137   _DEFINE_SPEC(is_unsigned, unsigned long)
138   _DEFINE_SPEC(is_unsigned, unsigned long long)
139
140   template<typename _Base, typename _Derived>
141     struct __is_base_of_helper
142     {
143       typedef typename remove_cv<_Base>::type    _NoCv_Base;
144       typedef typename remove_cv<_Derived>::type _NoCv_Derived;
145       static const bool __value = (is_same<_Base, _Derived>::value
146                                    || (__is_base_of(_Base, _Derived)
147                                        && !is_same<_NoCv_Base,
148                                                    _NoCv_Derived>::value));
149     };
150  
151   template<typename _Base, typename _Derived>
152     struct is_base_of
153     : public integral_constant<bool,
154                                __is_base_of_helper<_Base, _Derived>::__value>
155     { };
156
157   template<typename _From, typename _To>
158     struct __is_convertible_simple
159     : public __sfinae_types
160     {
161     private:
162       static __one __test(_To);
163       static __two __test(...);
164       static _From __makeFrom();
165     
166     public:
167       static const bool __value = sizeof(__test(__makeFrom())) == 1;
168     };
169
170   template<typename _Tp>
171     struct add_reference;
172
173   template<typename _Tp>
174     struct __is_int_or_cref
175     {
176       typedef typename remove_reference<_Tp>::type __rr_Tp;
177       static const bool __value = (is_integral<_Tp>::value
178                                    || (is_integral<__rr_Tp>::value
179                                        && is_const<__rr_Tp>::value
180                                        && !is_volatile<__rr_Tp>::value));
181     };
182
183   template<typename _From, typename _To,
184            bool = (is_void<_From>::value || is_void<_To>::value
185                    || is_function<_To>::value || is_array<_To>::value
186                    // This special case is here only to avoid warnings.            
187                    || (is_floating_point<typename
188                        remove_reference<_From>::type>::value
189                        && __is_int_or_cref<_To>::__value))>
190     struct __is_convertible_helper
191     {
192       // "An imaginary lvalue of type From...".
193       static const bool __value = (__is_convertible_simple<typename
194                                    add_reference<_From>::type, _To>::__value);
195     };
196
197   template<typename _From, typename _To>
198     struct __is_convertible_helper<_From, _To, true>
199     { static const bool __value = (is_void<_To>::value
200                                    || (__is_int_or_cref<_To>::__value
201                                        && !is_void<_From>::value)); };
202
203   template<typename _From, typename _To>
204     struct is_convertible
205     : public integral_constant<bool,
206                                __is_convertible_helper<_From, _To>::__value>
207     { };
208
209   /// @brief  reference modifications [4.7.2].
210   template<typename _Tp>
211     struct remove_reference
212     { typedef _Tp     type; };
213
214   template<typename _Tp>
215     struct remove_reference<_Tp&>
216     { typedef _Tp     type; };
217
218   // NB: Careful with reference to void.
219   template<typename _Tp, bool = (is_void<_Tp>::value
220                                  || is_reference<_Tp>::value)>
221     struct __add_reference_helper
222     { typedef _Tp&    type; };
223
224   template<typename _Tp>
225     struct __add_reference_helper<_Tp, true>
226     { typedef _Tp     type; };
227
228   template<typename _Tp>
229     struct add_reference
230     : public __add_reference_helper<_Tp>
231     { };
232
233   /// @brief  other transformations [4.8].
234   template<std::size_t _Len, std::size_t _Align>
235     struct aligned_storage
236     { 
237       union type
238       {
239         unsigned char __data[_Len];
240         struct __attribute__((__aligned__((_Align)))) { } __align; 
241       };
242     };
243
244 #undef _DEFINE_SPEC_HELPER
245 #undef _DEFINE_SPEC
246 }
247 }
248
249 #endif // _GLIBCXX_TR1_TYPE_TRAITS