libstdc++
type_traits.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the terms
8 // of the GNU General Public License as published by the Free Software
9 // Foundation; either version 3, or (at your option) any later
10 // version.
11 
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
16 
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 /** @file ext/type_traits.h
27  * This file is a GNU extension to the Standard C++ Library.
28  */
29 
30 #ifndef _EXT_TYPE_TRAITS
31 #define _EXT_TYPE_TRAITS 1
32 
33 #pragma GCC system_header
34 
35 #include <bits/c++config.h>
36 #include <bits/cpp_type_traits.h>
37 
38 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
39 {
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 
42  // Define a nested type if some predicate holds.
43  template<bool, typename>
44  struct __enable_if
45  { };
46 
47  template<typename _Tp>
48  struct __enable_if<true, _Tp>
49  { typedef _Tp __type; };
50 
51 
52  // Conditional expression for types. If true, first, if false, second.
53  template<bool _Cond, typename _Iftrue, typename _Iffalse>
54  struct __conditional_type
55  { typedef _Iftrue __type; };
56 
57  template<typename _Iftrue, typename _Iffalse>
58  struct __conditional_type<false, _Iftrue, _Iffalse>
59  { typedef _Iffalse __type; };
60 
61 
62  // Given an integral builtin type, return the corresponding unsigned type.
63  template<typename _Tp>
64  struct __add_unsigned
65  {
66  private:
67  typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type;
68 
69  public:
70  typedef typename __if_type::__type __type;
71  };
72 
73  template<>
74  struct __add_unsigned<char>
75  { typedef unsigned char __type; };
76 
77  template<>
78  struct __add_unsigned<signed char>
79  { typedef unsigned char __type; };
80 
81  template<>
82  struct __add_unsigned<short>
83  { typedef unsigned short __type; };
84 
85  template<>
86  struct __add_unsigned<int>
87  { typedef unsigned int __type; };
88 
89  template<>
90  struct __add_unsigned<long>
91  { typedef unsigned long __type; };
92 
93  template<>
94  struct __add_unsigned<long long>
95  { typedef unsigned long long __type; };
96 
97  // Declare but don't define.
98  template<>
99  struct __add_unsigned<bool>;
100 
101  template<>
102  struct __add_unsigned<wchar_t>;
103 
104 
105  // Given an integral builtin type, return the corresponding signed type.
106  template<typename _Tp>
107  struct __remove_unsigned
108  {
109  private:
110  typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type;
111 
112  public:
113  typedef typename __if_type::__type __type;
114  };
115 
116  template<>
117  struct __remove_unsigned<char>
118  { typedef signed char __type; };
119 
120  template<>
121  struct __remove_unsigned<unsigned char>
122  { typedef signed char __type; };
123 
124  template<>
125  struct __remove_unsigned<unsigned short>
126  { typedef short __type; };
127 
128  template<>
129  struct __remove_unsigned<unsigned int>
130  { typedef int __type; };
131 
132  template<>
133  struct __remove_unsigned<unsigned long>
134  { typedef long __type; };
135 
136  template<>
137  struct __remove_unsigned<unsigned long long>
138  { typedef long long __type; };
139 
140  // Declare but don't define.
141  template<>
142  struct __remove_unsigned<bool>;
143 
144  template<>
145  struct __remove_unsigned<wchar_t>;
146 
147 
148  // For use in string and vstring.
149  template<typename _Type>
150  inline bool
151  __is_null_pointer(_Type* __ptr)
152  { return __ptr == 0; }
153 
154  template<typename _Type>
155  inline bool
156  __is_null_pointer(_Type)
157  { return false; }
158 
159 
160  // For complex and cmath
161  template<typename _Tp, bool = std::__is_integer<_Tp>::__value>
162  struct __promote
163  { typedef double __type; };
164 
165  // No nested __type member for non-integer non-floating point types,
166  // allows this type to be used for SFINAE to constrain overloads in
167  // <cmath> and <complex> to only the intended types.
168  template<typename _Tp>
169  struct __promote<_Tp, false>
170  { };
171 
172  template<>
173  struct __promote<long double>
174  { typedef long double __type; };
175 
176  template<>
177  struct __promote<double>
178  { typedef double __type; };
179 
180  template<>
181  struct __promote<float>
182  { typedef float __type; };
183 
184  template<typename _Tp, typename _Up,
185  typename _Tp2 = typename __promote<_Tp>::__type,
186  typename _Up2 = typename __promote<_Up>::__type>
187  struct __promote_2
188  {
189  typedef __typeof__(_Tp2() + _Up2()) __type;
190  };
191 
192  template<typename _Tp, typename _Up, typename _Vp,
193  typename _Tp2 = typename __promote<_Tp>::__type,
194  typename _Up2 = typename __promote<_Up>::__type,
195  typename _Vp2 = typename __promote<_Vp>::__type>
196  struct __promote_3
197  {
198  typedef __typeof__(_Tp2() + _Up2() + _Vp2()) __type;
199  };
200 
201  template<typename _Tp, typename _Up, typename _Vp, typename _Wp,
202  typename _Tp2 = typename __promote<_Tp>::__type,
203  typename _Up2 = typename __promote<_Up>::__type,
204  typename _Vp2 = typename __promote<_Vp>::__type,
205  typename _Wp2 = typename __promote<_Wp>::__type>
206  struct __promote_4
207  {
208  typedef __typeof__(_Tp2() + _Up2() + _Vp2() + _Wp2()) __type;
209  };
210 
211 _GLIBCXX_END_NAMESPACE_VERSION
212 } // namespace
213 
214 #endif