3 // Copyright (C) 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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // 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
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 /** @file ext/type_traits.h
32 * This file is a GNU extension to the Standard C++ Library.
35 #ifndef _EXT_TYPE_TRAITS
36 #define _EXT_TYPE_TRAITS 1
38 #pragma GCC system_header
41 #include <bits/cpp_type_traits.h>
43 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
45 // Define a nested type if some predicate holds.
46 template<bool, typename
>
50 template<typename _Tp
>
51 struct __enable_if
<true, _Tp
>
52 { typedef _Tp __type
; };
55 // Conditional expression for types. If true, first, if false, second.
56 template<bool _Cond
, typename _Iftrue
, typename _Iffalse
>
57 struct __conditional_type
58 { typedef _Iftrue __type
; };
60 template<typename _Iftrue
, typename _Iffalse
>
61 struct __conditional_type
<false, _Iftrue
, _Iffalse
>
62 { typedef _Iffalse __type
; };
65 // Given an integral builtin type, return the corresponding unsigned type.
66 template<typename _Tp
>
70 typedef __enable_if
<std::__is_integer
<_Tp
>::__value
, _Tp
> __if_type
;
73 typedef typename
__if_type::__type __type
;
77 struct __add_unsigned
<char>
78 { typedef unsigned char __type
; };
81 struct __add_unsigned
<signed char>
82 { typedef unsigned char __type
; };
85 struct __add_unsigned
<short>
86 { typedef unsigned short __type
; };
89 struct __add_unsigned
<int>
90 { typedef unsigned int __type
; };
93 struct __add_unsigned
<long>
94 { typedef unsigned long __type
; };
97 struct __add_unsigned
<long long>
98 { typedef unsigned long long __type
; };
100 // Declare but don't define.
102 struct __add_unsigned
<bool>;
105 struct __add_unsigned
<wchar_t>;
108 // Given an integral builtin type, return the corresponding signed type.
109 template<typename _Tp
>
110 struct __remove_unsigned
113 typedef __enable_if
<std::__is_integer
<_Tp
>::__value
, _Tp
> __if_type
;
116 typedef typename
__if_type::__type __type
;
120 struct __remove_unsigned
<char>
121 { typedef signed char __type
; };
124 struct __remove_unsigned
<unsigned char>
125 { typedef signed char __type
; };
128 struct __remove_unsigned
<unsigned short>
129 { typedef short __type
; };
132 struct __remove_unsigned
<unsigned int>
133 { typedef int __type
; };
136 struct __remove_unsigned
<unsigned long>
137 { typedef long __type
; };
140 struct __remove_unsigned
<unsigned long long>
141 { typedef long long __type
; };
143 // Declare but don't define.
145 struct __remove_unsigned
<bool>;
148 struct __remove_unsigned
<wchar_t>;
151 // Compile time constants for builtin types.
152 // Sadly std::numeric_limits member functions cannot be used for this.
153 #define __glibcxx_signed(_Tp) ((_Tp)(-1) < 0)
154 #define __glibcxx_digits(_Tp) \
155 (sizeof(_Tp) * __CHAR_BIT__ - __glibcxx_signed(_Tp))
157 #define __glibcxx_min(_Tp) \
158 (__glibcxx_signed(_Tp) ? (_Tp)1 << __glibcxx_digits(_Tp) : (_Tp)0)
160 #define __glibcxx_max(_Tp) \
161 (__glibcxx_signed(_Tp) ? \
162 (((((_Tp)1 << (__glibcxx_digits(_Tp) - 1)) - 1) << 1) + 1) : ~(_Tp)0)
164 template<typename _Value
>
165 struct __numeric_traits_integer
167 // Only integers for initialization of member constant.
168 static const _Value __min
= __glibcxx_min(_Value
);
169 static const _Value __max
= __glibcxx_max(_Value
);
172 template<typename _Value
>
173 const _Value __numeric_traits_integer
<_Value
>::__min
;
175 template<typename _Value
>
176 const _Value __numeric_traits_integer
<_Value
>::__max
;
178 template<typename _Value
>
179 struct __numeric_traits_floating
181 // Only floating point types. See N1822.
182 static const int __max_digits10
=
183 2 + std::numeric_limits
<_Value
>::digits
* 3010/10000;
186 template<typename _Value
>
187 const int __numeric_traits_floating
<_Value
>::__max_digits10
;
189 template<typename _Value
>
190 struct __numeric_traits
191 : public __conditional_type
<std::__is_integer
<_Value
>::__value
,
192 __numeric_traits_integer
<_Value
>,
193 __numeric_traits_floating
<_Value
> >::__type
196 _GLIBCXX_END_NAMESPACE
198 #undef __glibcxx_signed
201 #undef __glibcxx_digits