1 /* boost integer_traits.hpp header file
\r
3 * Copyright Jens Maurer 2000
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * $Id: integer_traits.hpp 58381 2009-12-14 18:14:48Z johnmaddock $
\r
10 * Idea by Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers
\r
13 // See http://www.boost.org/libs/integer for documentation.
\r
16 #ifndef BOOST_INTEGER_TRAITS_HPP
\r
17 #define BOOST_INTEGER_TRAITS_HPP
\r
19 #include <boost/config.hpp>
\r
20 #include <boost/limits.hpp>
\r
22 // These are an implementation detail and not part of the interface
\r
24 // we need wchar.h for WCHAR_MAX/MIN but not all platforms provide it,
\r
25 // and some may have <wchar.h> but not <cwchar> ...
\r
26 #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) && (!defined(BOOST_NO_CWCHAR) || defined(sun) || defined(__sun) || defined(__QNX__))
\r
31 // We simply cannot include this header on gcc without getting copious warnings of the kind:
\r
33 // ../../../boost/integer_traits.hpp:164:66: warning: use of C99 long long integer constant
\r
35 // And yet there is no other reasonable implementation, so we declare this a system header
\r
36 // to suppress these warnings.
\r
38 #if defined(__GNUC__) && (__GNUC__ >= 4)
\r
39 #pragma GCC system_header
\r
44 class integer_traits : public std::numeric_limits<T>
\r
47 BOOST_STATIC_CONSTANT(bool, is_integral = false);
\r
51 template<class T, T min_val, T max_val>
\r
52 class integer_traits_base
\r
55 BOOST_STATIC_CONSTANT(bool, is_integral = true);
\r
56 BOOST_STATIC_CONSTANT(T, const_min = min_val);
\r
57 BOOST_STATIC_CONSTANT(T, const_max = max_val);
\r
60 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
\r
61 // A definition is required even for integral static constants
\r
62 template<class T, T min_val, T max_val>
\r
63 const bool integer_traits_base<T, min_val, max_val>::is_integral;
\r
65 template<class T, T min_val, T max_val>
\r
66 const T integer_traits_base<T, min_val, max_val>::const_min;
\r
68 template<class T, T min_val, T max_val>
\r
69 const T integer_traits_base<T, min_val, max_val>::const_max;
\r
72 } // namespace detail
\r
75 class integer_traits<bool>
\r
76 : public std::numeric_limits<bool>,
\r
77 public detail::integer_traits_base<bool, false, true>
\r
81 class integer_traits<char>
\r
82 : public std::numeric_limits<char>,
\r
83 public detail::integer_traits_base<char, CHAR_MIN, CHAR_MAX>
\r
87 class integer_traits<signed char>
\r
88 : public std::numeric_limits<signed char>,
\r
89 public detail::integer_traits_base<signed char, SCHAR_MIN, SCHAR_MAX>
\r
93 class integer_traits<unsigned char>
\r
94 : public std::numeric_limits<unsigned char>,
\r
95 public detail::integer_traits_base<unsigned char, 0, UCHAR_MAX>
\r
98 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
\r
100 class integer_traits<wchar_t>
\r
101 : public std::numeric_limits<wchar_t>,
\r
102 // Don't trust WCHAR_MIN and WCHAR_MAX with Mac OS X's native
\r
103 // library: they are wrong!
\r
104 #if defined(WCHAR_MIN) && defined(WCHAR_MAX) && !defined(__APPLE__)
\r
105 public detail::integer_traits_base<wchar_t, WCHAR_MIN, WCHAR_MAX>
\r
106 #elif defined(__BORLANDC__) || defined(__CYGWIN__) || defined(__MINGW32__) || (defined(__BEOS__) && defined(__GNUC__))
\r
107 // No WCHAR_MIN and WCHAR_MAX, whar_t is short and unsigned:
\r
108 public detail::integer_traits_base<wchar_t, 0, 0xffff>
\r
109 #elif (defined(__sgi) && (!defined(__SGI_STL_PORT) || __SGI_STL_PORT < 0x400))\
\r
110 || (defined __APPLE__)\
\r
111 || (defined(__OpenBSD__) && defined(__GNUC__))\
\r
112 || (defined(__NetBSD__) && defined(__GNUC__))\
\r
113 || (defined(__FreeBSD__) && defined(__GNUC__))\
\r
114 || (defined(__DragonFly__) && defined(__GNUC__))\
\r
115 || (defined(__hpux) && defined(__GNUC__) && (__GNUC__ == 3) && !defined(__SGI_STL_PORT))
\r
116 // No WCHAR_MIN and WCHAR_MAX, wchar_t has the same range as int.
\r
117 // - SGI MIPSpro with native library
\r
118 // - gcc 3.x on HP-UX
\r
119 // - Mac OS X with native library
\r
120 // - gcc on FreeBSD, OpenBSD and NetBSD
\r
121 public detail::integer_traits_base<wchar_t, INT_MIN, INT_MAX>
\r
122 #elif defined(__hpux) && defined(__GNUC__) && (__GNUC__ == 2) && !defined(__SGI_STL_PORT)
\r
123 // No WCHAR_MIN and WCHAR_MAX, wchar_t has the same range as unsigned int.
\r
124 // - gcc 2.95.x on HP-UX
\r
125 // (also, std::numeric_limits<wchar_t> appears to return the wrong values).
\r
126 public detail::integer_traits_base<wchar_t, 0, UINT_MAX>
\r
128 #error No WCHAR_MIN and WCHAR_MAX present, please adjust integer_traits<> for your compiler.
\r
131 #endif // BOOST_NO_INTRINSIC_WCHAR_T
\r
134 class integer_traits<short>
\r
135 : public std::numeric_limits<short>,
\r
136 public detail::integer_traits_base<short, SHRT_MIN, SHRT_MAX>
\r
140 class integer_traits<unsigned short>
\r
141 : public std::numeric_limits<unsigned short>,
\r
142 public detail::integer_traits_base<unsigned short, 0, USHRT_MAX>
\r
146 class integer_traits<int>
\r
147 : public std::numeric_limits<int>,
\r
148 public detail::integer_traits_base<int, INT_MIN, INT_MAX>
\r
152 class integer_traits<unsigned int>
\r
153 : public std::numeric_limits<unsigned int>,
\r
154 public detail::integer_traits_base<unsigned int, 0, UINT_MAX>
\r
158 class integer_traits<long>
\r
159 : public std::numeric_limits<long>,
\r
160 public detail::integer_traits_base<long, LONG_MIN, LONG_MAX>
\r
164 class integer_traits<unsigned long>
\r
165 : public std::numeric_limits<unsigned long>,
\r
166 public detail::integer_traits_base<unsigned long, 0, ULONG_MAX>
\r
169 #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T)
\r
170 #if defined(ULLONG_MAX) && defined(BOOST_HAS_LONG_LONG)
\r
173 class integer_traits< ::boost::long_long_type>
\r
174 : public std::numeric_limits< ::boost::long_long_type>,
\r
175 public detail::integer_traits_base< ::boost::long_long_type, LLONG_MIN, LLONG_MAX>
\r
179 class integer_traits< ::boost::ulong_long_type>
\r
180 : public std::numeric_limits< ::boost::ulong_long_type>,
\r
181 public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULLONG_MAX>
\r
184 #elif defined(ULONG_LONG_MAX) && defined(BOOST_HAS_LONG_LONG)
\r
187 class integer_traits< ::boost::long_long_type> : public std::numeric_limits< ::boost::long_long_type>, public detail::integer_traits_base< ::boost::long_long_type, LONG_LONG_MIN, LONG_LONG_MAX>{ };
\r
189 class integer_traits< ::boost::ulong_long_type>
\r
190 : public std::numeric_limits< ::boost::ulong_long_type>,
\r
191 public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULONG_LONG_MAX>
\r
194 #elif defined(ULONGLONG_MAX) && defined(BOOST_HAS_LONG_LONG)
\r
197 class integer_traits< ::boost::long_long_type>
\r
198 : public std::numeric_limits< ::boost::long_long_type>,
\r
199 public detail::integer_traits_base< ::boost::long_long_type, LONGLONG_MIN, LONGLONG_MAX>
\r
203 class integer_traits< ::boost::ulong_long_type>
\r
204 : public std::numeric_limits< ::boost::ulong_long_type>,
\r
205 public detail::integer_traits_base< ::boost::ulong_long_type, 0, ULONGLONG_MAX>
\r
208 #elif defined(_LLONG_MAX) && defined(_C2) && defined(BOOST_HAS_LONG_LONG)
\r
211 class integer_traits< ::boost::long_long_type>
\r
212 : public std::numeric_limits< ::boost::long_long_type>,
\r
213 public detail::integer_traits_base< ::boost::long_long_type, -_LLONG_MAX - _C2, _LLONG_MAX>
\r
217 class integer_traits< ::boost::ulong_long_type>
\r
218 : public std::numeric_limits< ::boost::ulong_long_type>,
\r
219 public detail::integer_traits_base< ::boost::ulong_long_type, 0, _ULLONG_MAX>
\r
222 #elif defined(BOOST_HAS_LONG_LONG)
\r
224 // we have long long but no constants, this happens for example with gcc in -ansi mode,
\r
225 // we'll just have to work out the values for ourselves (assumes 2's compliment representation):
\r
228 class integer_traits< ::boost::long_long_type>
\r
229 : public std::numeric_limits< ::boost::long_long_type>,
\r
230 public detail::integer_traits_base< ::boost::long_long_type, (1LL << (sizeof(::boost::long_long_type) - 1)), ~(1LL << (sizeof(::boost::long_long_type) - 1))>
\r
234 class integer_traits< ::boost::ulong_long_type>
\r
235 : public std::numeric_limits< ::boost::ulong_long_type>,
\r
236 public detail::integer_traits_base< ::boost::ulong_long_type, 0, ~0uLL>
\r
239 #elif defined(BOOST_HAS_MS_INT64)
\r
242 class integer_traits< __int64>
\r
243 : public std::numeric_limits< __int64>,
\r
244 public detail::integer_traits_base< __int64, _I64_MIN, _I64_MAX>
\r
248 class integer_traits< unsigned __int64>
\r
249 : public std::numeric_limits< unsigned __int64>,
\r
250 public detail::integer_traits_base< unsigned __int64, 0, _UI64_MAX>
\r
256 } // namespace boost
\r
258 #endif /* BOOST_INTEGER_TRAITS_HPP */
\r