1 // TR1 type_traits -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006, 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/type_traits
31 * This is a TR1 C++ Library header.
34 #ifndef _GLIBCXX_TR1_TYPE_TRAITS
35 #define _GLIBCXX_TR1_TYPE_TRAITS 1
37 #pragma GCC system_header
39 #if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
40 # error TR1 header cannot be included from C++0x header
45 #if defined(_GLIBCXX_INCLUDE_AS_TR1)
46 # include <tr1_impl/type_traits>
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>
54 # undef _GLIBCXX_END_NAMESPACE_TR1
55 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
56 # undef _GLIBCXX_INCLUDE_AS_TR1
63 #define _DEFINE_SPEC_HELPER(_Spec) \
66 : public true_type { };
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>)
76 : public false_type { };
78 template<typename _Tp>
79 struct is_reference<_Tp&>
80 : public true_type { };
82 template<typename _Tp>
84 : public integral_constant<bool, __is_pod(_Tp) || is_void<_Tp>::value>
87 template<typename _Tp>
88 struct has_trivial_constructor
89 : public integral_constant<bool, is_pod<_Tp>::value>
92 template<typename _Tp>
93 struct has_trivial_copy
94 : public integral_constant<bool, is_pod<_Tp>::value>
97 template<typename _Tp>
98 struct has_trivial_assign
99 : public integral_constant<bool, is_pod<_Tp>::value>
102 template<typename _Tp>
103 struct has_trivial_destructor
104 : public integral_constant<bool, is_pod<_Tp>::value>
107 template<typename _Tp>
108 struct has_nothrow_constructor
109 : public integral_constant<bool, is_pod<_Tp>::value>
112 template<typename _Tp>
113 struct has_nothrow_copy
114 : public integral_constant<bool, is_pod<_Tp>::value>
117 template<typename _Tp>
118 struct has_nothrow_assign
119 : public integral_constant<bool, is_pod<_Tp>::value>
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)
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)
140 template<typename _Base, typename _Derived>
141 struct __is_base_of_helper
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));
151 template<typename _Base, typename _Derived>
153 : public integral_constant<bool,
154 __is_base_of_helper<_Base, _Derived>::__value>
157 template<typename _From, typename _To>
158 struct __is_convertible_simple
159 : public __sfinae_types
162 static __one __test(_To);
163 static __two __test(...);
164 static _From __makeFrom();
167 static const bool __value = sizeof(__test(__makeFrom())) == 1;
170 template<typename _Tp>
171 struct add_reference;
173 template<typename _Tp>
174 struct __is_int_or_cref
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));
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
192 // "An imaginary lvalue of type From...".
193 static const bool __value = (__is_convertible_simple<typename
194 add_reference<_From>::type, _To>::__value);
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)); };
203 template<typename _From, typename _To>
204 struct is_convertible
205 : public integral_constant<bool,
206 __is_convertible_helper<_From, _To>::__value>
209 /// @brief reference modifications [4.7.2].
210 template<typename _Tp>
211 struct remove_reference
212 { typedef _Tp type; };
214 template<typename _Tp>
215 struct remove_reference<_Tp&>
216 { typedef _Tp type; };
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; };
224 template<typename _Tp>
225 struct __add_reference_helper<_Tp, true>
226 { typedef _Tp type; };
228 template<typename _Tp>
230 : public __add_reference_helper<_Tp>
233 /// @brief other transformations [4.8].
234 template<std::size_t _Len, std::size_t _Align>
235 struct aligned_storage
239 unsigned char __data[_Len];
240 struct __attribute__((__aligned__((_Align)))) { } __align;
244 #undef _DEFINE_SPEC_HELPER
249 #endif // _GLIBCXX_TR1_TYPE_TRAITS