OSDN Git Service

2002-01-16 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / cpp_type_traits.h
1 // The  -*- C++ -*- type traits classes for internal use in libstdc++
2
3 // Copyright (C) 2000, 2001 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 // Written by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
31
32 /** @file cpp_type_traits.h
33  *  This is an internal header file, included by other library headers.
34  *  You should not attempt to use it directly.
35  */
36
37 #ifndef _CPP_BITS_CPP_TYPE_TRAITS_H
38 #define _CPP_BITS_CPP_TYPE_TRAITS_H 1
39
40 #pragma GCC system_header
41
42 //
43 // This file provides some compile-time information about various types.
44 // These representations were designed, on purpose, to be constant-expressions
45 // and not types as found in <stl/bits/type_traits.h>.  In particular, they
46 // can be used in control structures and the optimizer hopefully will do
47 // the obvious thing.
48 //
49 // Why integral expressions, and not functions nor types?
50 // Firstly, these compile-time entities are used as template-arguments
51 // so function return values won't work:  We need compile-time entities.
52 // We're left with types and constant  integral expressions.
53 // Secondly, from the point of view of ease of use, type-based compile-time
54 // information is -not- *that* convenient.  On has to write lots of
55 // overloaded functions and to hope that the compiler will select the right
56 // one. As a net effect, the overall structure isn't very clear at first
57 // glance.
58 // Thirdly, partial ordering and overload resolution (of function templates)
59 // is highly costly in terms of compiler-resource.  It is a Good Thing to
60 // keep these resource consumption as least as possible.
61 //
62 // See valarray_array.h for a case use.
63 //
64 // -- Gaby (dosreis@cmla.ens-cachan.fr) 2000-03-06.
65 //
66
67 namespace std
68 {
69   template<typename _Tp>
70     struct __is_void
71     {
72       enum
73       {
74         _M_type = 0
75       };
76     };
77
78   template<>
79     struct __is_void<void>
80     {
81       enum
82       {
83         _M_type = 1
84       };
85     };
86
87   //
88   // Integer types
89   //
90   template<typename _Tp>
91     struct __is_integer
92     {
93       enum
94       {
95         _M_type = 0
96       };
97     };
98
99   // Thirteen specializations (yes there are eleven standard integer
100   // types; 'long long' and 'unsigned long long' are supported as
101   // extensions)
102   template<>
103     struct __is_integer<bool>
104     {
105       enum
106       {
107         _M_type = 1
108       };
109     };
110   
111   template<>
112     struct __is_integer<char>
113     {
114       enum
115       {
116         _M_type = 1
117       };
118     };
119
120   template<>
121     struct __is_integer<signed char>
122     {
123       enum
124       {
125         _M_type = 1
126       };
127     };
128   
129   template<>
130   struct __is_integer<unsigned char>
131   {
132     enum
133     {
134       _M_type = 1
135     };
136   };
137
138 # ifdef _GLIBCPP_USE_WCHAR_T
139   template<>
140   struct __is_integer<wchar_t>
141   {
142     enum
143     {
144       _M_type = 1
145     };
146   };
147 # endif
148   
149   template<>
150   struct __is_integer<short>
151   {
152     enum
153     {
154       _M_type = 1
155     };
156   };
157
158   template<>
159   struct __is_integer<unsigned short>
160   {
161     enum
162     {
163       _M_type = 1
164     };
165   };
166
167   template<>
168   struct __is_integer<int>
169   {
170     enum
171     {
172       _M_type = 1
173     };
174   };
175
176   template<>
177   struct __is_integer<unsigned int>
178   {
179     enum
180     {
181       _M_type = 1
182     };
183   };
184
185   template<>
186   struct __is_integer<long>
187   {
188     enum
189     {
190       _M_type = 1
191     };
192   };
193
194   template<>
195   struct __is_integer<unsigned long>
196   {
197     enum
198     {
199       _M_type = 1
200     };
201   };
202
203   template<>
204   struct __is_integer<long long>
205   {
206     enum
207     {
208       _M_type = 1
209     };
210   };
211
212   template<>
213   struct __is_integer<unsigned long long>
214   {
215     enum
216     {
217       _M_type = 1
218     };
219   };
220
221   //
222   // Floating point types
223   //
224   template<typename _Tp>
225   struct __is_floating
226   {
227     enum
228     {
229       _M_type = 0
230     };
231   };
232
233   // three specializations (float, double and 'long double')
234   template<>
235   struct __is_floating<float>
236   {
237     enum
238     {
239       _M_type = 1
240     };
241   };
242
243   template<>
244   struct __is_floating<double>
245   {
246     enum
247     {
248       _M_type = 1
249     };
250   };
251
252   template<>
253   struct __is_floating<long double>
254   {
255     enum
256     {
257       _M_type = 1
258     };
259   };
260
261   //
262   // An arithmetic type is an integer type or a floating point type
263   //
264   template<typename _Tp>
265   struct __is_arithmetic
266   {
267     enum
268     {
269       _M_type = __is_integer<_Tp>::_M_type || __is_floating<_Tp>::_M_type
270     };
271   };
272
273   //
274   // A fundamental type is `void' or and arithmetic type
275   //
276   template<typename _Tp>
277   struct __is_fundamental
278   {
279     enum
280     {
281       _M_type = __is_void<_Tp>::_M_type || __is_arithmetic<_Tp>::_M_type
282     };
283   };
284
285   //
286   // For the immediate use, the following is a good approximation
287   //
288   template<typename _Tp>
289   struct __is_pod
290   {
291     enum
292     {
293       _M_type = __is_fundamental<_Tp>::_M_type
294     };
295   };
296
297 } // namespace std
298
299
300 #endif //_CPP_BITS_CPP_TYPE_TRAITS_H