1 // TR1 type_traits -*- C++ -*-
3 // Copyright (C) 2007 Free Software Foundation, Inc.
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)
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.
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,
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.
30 /** @file tr1_impl/type_traits
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
35 #include <tr1_impl/type_traitsfwd.h>
39 _GLIBCXX_BEGIN_NAMESPACE_TR1
41 // For use in __is_convertible_simple.
45 typedef struct { char __arr[2]; } __two;
48 #define _DEFINE_SPEC_BODY(_Value) \
49 : public integral_constant<bool, _Value> { };
51 #define _DEFINE_SPEC_0_HELPER(_Spec, _Value) \
54 _DEFINE_SPEC_BODY(_Value)
56 #define _DEFINE_SPEC_1_HELPER(_Spec, _Value) \
57 template<typename _Tp> \
59 _DEFINE_SPEC_BODY(_Value)
61 #define _DEFINE_SPEC_2_HELPER(_Spec, _Value) \
62 template<typename _Tp, typename _Cp> \
64 _DEFINE_SPEC_BODY(_Value)
66 #define _DEFINE_SPEC(_Order, _Trait, _Type, _Value) \
67 _DEFINE_SPEC_##_Order##_HELPER(_Trait<_Type>, _Value) \
68 _DEFINE_SPEC_##_Order##_HELPER(_Trait<_Type const>, _Value) \
69 _DEFINE_SPEC_##_Order##_HELPER(_Trait<_Type volatile>, _Value) \
70 _DEFINE_SPEC_##_Order##_HELPER(_Trait<_Type const volatile>, _Value)
72 /// @brief helper classes [4.3].
73 template<typename _Tp, _Tp __v>
74 struct integral_constant
76 static const _Tp value = __v;
77 typedef _Tp value_type;
78 typedef integral_constant<_Tp, __v> type;
80 typedef integral_constant<bool, true> true_type;
81 typedef integral_constant<bool, false> false_type;
83 template<typename _Tp, _Tp __v>
84 const _Tp integral_constant<_Tp, __v>::value;
86 /// @brief primary type categories [4.5.1].
89 : public false_type { };
90 _DEFINE_SPEC(0, is_void, void, true)
94 : public false_type { };
95 _DEFINE_SPEC(0, is_integral, bool, true)
96 _DEFINE_SPEC(0, is_integral, char, true)
97 _DEFINE_SPEC(0, is_integral, signed char, true)
98 _DEFINE_SPEC(0, is_integral, unsigned char, true)
99 #ifdef _GLIBCXX_USE_WCHAR_T
100 _DEFINE_SPEC(0, is_integral, wchar_t, true)
102 _DEFINE_SPEC(0, is_integral, short, true)
103 _DEFINE_SPEC(0, is_integral, unsigned short, true)
104 _DEFINE_SPEC(0, is_integral, int, true)
105 _DEFINE_SPEC(0, is_integral, unsigned int, true)
106 _DEFINE_SPEC(0, is_integral, long, true)
107 _DEFINE_SPEC(0, is_integral, unsigned long, true)
108 _DEFINE_SPEC(0, is_integral, long long, true)
109 _DEFINE_SPEC(0, is_integral, unsigned long long, true)
112 struct is_floating_point
113 : public false_type { };
114 _DEFINE_SPEC(0, is_floating_point, float, true)
115 _DEFINE_SPEC(0, is_floating_point, double, true)
116 _DEFINE_SPEC(0, is_floating_point, long double, true)
120 : public false_type { };
122 template<typename _Tp, std::size_t _Size>
123 struct is_array<_Tp[_Size]>
124 : public true_type { };
126 template<typename _Tp>
127 struct is_array<_Tp[]>
128 : public true_type { };
132 : public false_type { };
133 _DEFINE_SPEC(1, is_pointer, _Tp*, true)
136 struct is_member_object_pointer
137 : public false_type { };
138 _DEFINE_SPEC(2, is_member_object_pointer, _Tp _Cp::*,
139 !is_function<_Tp>::value)
142 struct is_member_function_pointer
143 : public false_type { };
144 _DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*,
145 is_function<_Tp>::value)
147 template<typename _Tp>
149 : public integral_constant<bool, __is_enum(_Tp)>
152 template<typename _Tp>
154 : public integral_constant<bool, __is_union(_Tp)>
157 template<typename _Tp>
159 : public integral_constant<bool, __is_class(_Tp)>
163 struct __is_function_helper
164 : public false_type { };
166 template<typename _Res, typename... _ArgTypes>
167 struct __is_function_helper<_Res(_ArgTypes...)>
168 : public true_type { };
170 template<typename _Res, typename... _ArgTypes>
171 struct __is_function_helper<_Res(_ArgTypes......)>
172 : public true_type { };
174 template<typename _Tp>
176 : public integral_constant<bool, (__is_function_helper<typename
177 remove_cv<_Tp>::type>::value)>
180 /// @brief composite type traits [4.5.2].
181 template<typename _Tp>
183 : public integral_constant<bool, (is_integral<_Tp>::value
184 || is_floating_point<_Tp>::value)>
187 template<typename _Tp>
188 struct is_fundamental
189 : public integral_constant<bool, (is_arithmetic<_Tp>::value
190 || is_void<_Tp>::value)>
193 template<typename _Tp>
195 : public integral_constant<bool, !(is_function<_Tp>::value
196 || is_reference<_Tp>::value
197 || is_void<_Tp>::value)>
200 template<typename _Tp>
202 : public integral_constant<bool, (is_arithmetic<_Tp>::value
203 || is_enum<_Tp>::value
204 || is_pointer<_Tp>::value
205 || is_member_pointer<_Tp>::value)>
208 template<typename _Tp>
210 : public integral_constant<bool, !is_fundamental<_Tp>::value> { };
212 template<typename _Tp>
213 struct is_member_pointer
214 : public integral_constant<bool,
215 (is_member_object_pointer<_Tp>::value
216 || is_member_function_pointer<_Tp>::value)>
219 /// @brief type properties [4.5.3].
222 : public false_type { };
224 template<typename _Tp>
225 struct is_const<_Tp const>
226 : public true_type { };
230 : public false_type { };
232 template<typename _Tp>
233 struct is_volatile<_Tp volatile>
234 : public true_type { };
236 template<typename _Tp>
238 : public integral_constant<bool, __is_empty(_Tp)>
241 template<typename _Tp>
242 struct is_polymorphic
243 : public integral_constant<bool, __is_polymorphic(_Tp)>
246 template<typename _Tp>
248 : public integral_constant<bool, __is_abstract(_Tp)>
251 template<typename _Tp>
252 struct has_virtual_destructor
253 : public integral_constant<bool, __has_virtual_destructor(_Tp)>
256 template<typename _Tp>
258 : public integral_constant<std::size_t, __alignof__(_Tp)> { };
262 : public integral_constant<std::size_t, 0> { };
264 template<typename _Tp, std::size_t _Size>
265 struct rank<_Tp[_Size]>
266 : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { };
268 template<typename _Tp>
270 : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { };
272 template<typename, unsigned>
274 : public integral_constant<std::size_t, 0> { };
276 template<typename _Tp, unsigned _Uint, std::size_t _Size>
277 struct extent<_Tp[_Size], _Uint>
278 : public integral_constant<std::size_t,
279 _Uint == 0 ? _Size : extent<_Tp,
283 template<typename _Tp, unsigned _Uint>
284 struct extent<_Tp[], _Uint>
285 : public integral_constant<std::size_t,
286 _Uint == 0 ? 0 : extent<_Tp,
290 /// @brief relationships between types [4.6].
291 template<typename, typename>
293 : public false_type { };
295 template<typename _Tp>
296 struct is_same<_Tp, _Tp>
297 : public true_type { };
299 /// @brief const-volatile modifications [4.7.1].
300 template<typename _Tp>
302 { typedef _Tp type; };
304 template<typename _Tp>
305 struct remove_const<_Tp const>
306 { typedef _Tp type; };
308 template<typename _Tp>
309 struct remove_volatile
310 { typedef _Tp type; };
312 template<typename _Tp>
313 struct remove_volatile<_Tp volatile>
314 { typedef _Tp type; };
316 template<typename _Tp>
320 remove_const<typename remove_volatile<_Tp>::type>::type type;
323 template<typename _Tp>
325 { typedef _Tp const type; };
327 template<typename _Tp>
329 { typedef _Tp volatile type; };
331 template<typename _Tp>
335 add_const<typename add_volatile<_Tp>::type>::type type;
338 /// @brief array modifications [4.7.3].
339 template<typename _Tp>
341 { typedef _Tp type; };
343 template<typename _Tp, std::size_t _Size>
344 struct remove_extent<_Tp[_Size]>
345 { typedef _Tp type; };
347 template<typename _Tp>
348 struct remove_extent<_Tp[]>
349 { typedef _Tp type; };
351 template<typename _Tp>
352 struct remove_all_extents
353 { typedef _Tp type; };
355 template<typename _Tp, std::size_t _Size>
356 struct remove_all_extents<_Tp[_Size]>
357 { typedef typename remove_all_extents<_Tp>::type type; };
359 template<typename _Tp>
360 struct remove_all_extents<_Tp[]>
361 { typedef typename remove_all_extents<_Tp>::type type; };
363 /// @brief pointer modifications [4.7.4].
364 #undef _DEFINE_SPEC_BODY
365 #define _DEFINE_SPEC_BODY(_Value) \
366 { typedef _Tp type; };
368 template<typename _Tp>
369 struct remove_pointer
370 { typedef _Tp type; };
371 _DEFINE_SPEC(1, remove_pointer, _Tp*, false)
373 template<typename _Tp>
375 { typedef typename remove_reference<_Tp>::type* type; };
377 /// @brief other transformations [4.8].
378 template<std::size_t _Len, std::size_t _Align>
379 struct aligned_storage
383 unsigned char __data[_Len];
384 struct __attribute__((__aligned__((_Align)))) { } __aligner;
388 #undef _DEFINE_SPEC_0_HELPER
389 #undef _DEFINE_SPEC_1_HELPER
390 #undef _DEFINE_SPEC_2_HELPER
392 #undef _DEFINE_SPEC_BODY
394 _GLIBCXX_END_NAMESPACE_TR1