1 // boost cast.hpp header file ----------------------------------------------//
3 // (C) Copyright Kevlin Henney and Dave Abrahams 1999.
4 // Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org/libs/conversion for Documentation.
11 // 23 JUN 05 Code extracted from /boost/cast.hpp into this new header.
12 // Keeps this legacy version of numeric_cast<> for old compilers
13 // wich can't compile the new version in /boost/numeric/conversion/cast.hpp
14 // (Fernando Cacciola)
15 // 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
16 // <boost/limits.hpp> instead (the workaround did not
17 // actually compile when BOOST_NO_LIMITS was defined in
18 // any case, so we loose nothing). (John Maddock)
19 // 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never
20 // worked with stock GCC; trying to get it to do that broke
22 // 20 Jan 01 Moved BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS to config.hpp.
23 // Removed unused BOOST_EXPLICIT_TARGET macro. Moved
24 // boost::detail::type to boost/type.hpp. Made it compile with
25 // stock gcc again (Dave Abrahams)
26 // 29 Nov 00 Remove nested namespace cast, cleanup spacing before Formal
27 // Review (Beman Dawes)
28 // 19 Oct 00 Fix numeric_cast for floating-point types (Dave Abrahams)
29 // 15 Jul 00 Suppress numeric_cast warnings for GCC, Borland and MSVC
31 // 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams)
32 // 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes)
33 // 27 Jun 00 More MSVC6 workarounds
34 // 15 Jun 00 Add workarounds for MSVC6
35 // 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov)
36 // 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar)
37 // 29 Dec 99 Change using declarations so usages in other namespaces work
38 // correctly (Dave Abrahams)
39 // 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors
40 // as suggested Darin Adler and improved by Valentin Bonnard.
41 // 2 Sep 99 Remove controversial asserts, simplify, rename.
42 // 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast,
43 // place in nested namespace.
44 // 3 Aug 99 Initial version
46 #ifndef BOOST_OLD_NUMERIC_CAST_HPP
47 #define BOOST_OLD_NUMERIC_CAST_HPP
49 # include <boost/config.hpp>
52 # include <boost/type.hpp>
53 # include <boost/limits.hpp>
54 # include <boost/numeric/conversion/converter_policies.hpp>
56 // It has been demonstrated numerous times that MSVC 6.0 fails silently at link
57 // time if you use a template function which has template parameters that don't
58 // appear in the function's argument list.
60 // TODO: Add this to config.hpp?
61 // FLC: This macro is repeated in boost/cast.hpp but only locally (is undefined at the bottom)
62 // so is OK to reproduce it here.
63 # if defined(BOOST_MSVC) && BOOST_MSVC < 1300
64 # define BOOST_EXPLICIT_DEFAULT_TARGET , ::boost::type<Target>* = 0
66 # define BOOST_EXPLICIT_DEFAULT_TARGET
71 using numeric::bad_numeric_cast;
73 // LEGACY numeric_cast [only for some old broken compilers] --------------------------------------//
75 // Contributed by Kevlin Henney
77 // numeric_cast ------------------------------------------------------------//
79 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(BOOST_SGI_CPP_LIMITS)
84 struct signed_numeric_limits : std::numeric_limits<T>
86 static inline T min BOOST_PREVENT_MACRO_SUBSTITUTION ()
88 return (std::numeric_limits<T>::min)() >= 0
89 // unary minus causes integral promotion, thus the static_cast<>
90 ? static_cast<T>(-(std::numeric_limits<T>::max)())
91 : (std::numeric_limits<T>::min)();
95 // Move to namespace boost in utility.hpp?
96 template <class T, bool specialized>
97 struct fixed_numeric_limits_base
98 : public if_true< std::numeric_limits<T>::is_signed >
99 ::BOOST_NESTED_TEMPLATE then< signed_numeric_limits<T>,
100 std::numeric_limits<T>
105 struct fixed_numeric_limits
106 : fixed_numeric_limits_base<T,(std::numeric_limits<T>::is_specialized)>
109 # ifdef BOOST_HAS_LONG_LONG
110 // cover implementations which supply no specialization for long
111 // long / unsigned long long. Not intended to be full
112 // numeric_limits replacements, but good enough for numeric_cast<>
114 struct fixed_numeric_limits_base< ::boost::long_long_type, false>
116 BOOST_STATIC_CONSTANT(bool, is_specialized = true);
117 BOOST_STATIC_CONSTANT(bool, is_signed = true);
118 static ::boost::long_long_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
123 return 9223372036854775807LL; // hope this is portable
127 static ::boost::long_long_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
132 return -( 9223372036854775807LL )-1; // hope this is portable
138 struct fixed_numeric_limits_base< ::boost::ulong_long_type, false>
140 BOOST_STATIC_CONSTANT(bool, is_specialized = true);
141 BOOST_STATIC_CONSTANT(bool, is_signed = false);
142 static ::boost::ulong_long_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
144 # ifdef ULONGLONG_MAX
145 return ULONGLONG_MAX;
147 return 0xffffffffffffffffULL; // hope this is portable
151 static ::boost::ulong_long_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
154 } // namespace detail
156 // less_than_type_min -
157 // x_is_signed should be numeric_limits<X>::is_signed
158 // y_is_signed should be numeric_limits<Y>::is_signed
159 // y_min should be numeric_limits<Y>::min()
161 // check(x, y_min) returns true iff x < y_min without invoking comparisons
162 // between signed and unsigned values.
164 // "poor man's partial specialization" is in use here.
165 template <bool x_is_signed, bool y_is_signed>
166 struct less_than_type_min
168 template <class X, class Y>
169 static bool check(X x, Y y_min)
170 { return x < y_min; }
174 struct less_than_type_min<false, true>
176 template <class X, class Y>
177 static bool check(X, Y)
182 struct less_than_type_min<true, false>
184 template <class X, class Y>
185 static bool check(X x, Y)
189 // greater_than_type_max -
190 // same_sign should be:
191 // numeric_limits<X>::is_signed == numeric_limits<Y>::is_signed
192 // y_max should be numeric_limits<Y>::max()
194 // check(x, y_max) returns true iff x > y_max without invoking comparisons
195 // between signed and unsigned values.
197 // "poor man's partial specialization" is in use here.
198 template <bool same_sign, bool x_is_signed>
199 struct greater_than_type_max;
202 struct greater_than_type_max<true, true>
204 template <class X, class Y>
205 static inline bool check(X x, Y y_max)
206 { return x > y_max; }
210 struct greater_than_type_max<false, true>
212 // What does the standard say about this? I think it's right, and it
213 // will work with every compiler I know of.
214 template <class X, class Y>
215 static inline bool check(X x, Y)
216 { return x >= 0 && static_cast<X>(static_cast<Y>(x)) != x; }
218 # if defined(BOOST_MSVC) && BOOST_MSVC < 1300
219 // MSVC6 can't static_cast unsigned __int64 -> floating types
220 # define BOOST_UINT64_CAST(src_type) \
221 static inline bool check(src_type x, unsigned __int64) \
223 if (x < 0) return false; \
224 unsigned __int64 y = static_cast<unsigned __int64>(x); \
225 bool odd = y & 0x1; \
226 __int64 div2 = static_cast<__int64>(y >> 1); \
227 return ((static_cast<src_type>(div2) * 2.0) + odd) != x; \
230 BOOST_UINT64_CAST(long double);
231 BOOST_UINT64_CAST(double);
232 BOOST_UINT64_CAST(float);
233 # undef BOOST_UINT64_CAST
238 struct greater_than_type_max<true, false>
240 template <class X, class Y>
241 static inline bool check(X x, Y y_max)
242 { return x > y_max; }
246 struct greater_than_type_max<false, false>
248 // What does the standard say about this? I think it's right, and it
249 // will work with every compiler I know of.
250 template <class X, class Y>
251 static inline bool check(X x, Y)
252 { return static_cast<X>(static_cast<Y>(x)) != x; }
255 #else // use #pragma hacks if available
260 # pragma warning(push)
261 # pragma warning(disable : 4018)
262 # pragma warning(disable : 4146)
263 #elif defined(__BORLANDC__)
264 # pragma option push -w-8041
267 // Move to namespace boost in utility.hpp?
269 struct fixed_numeric_limits : public std::numeric_limits<T>
271 static inline T min BOOST_PREVENT_MACRO_SUBSTITUTION ()
273 return std::numeric_limits<T>::is_signed && (std::numeric_limits<T>::min)() >= 0
274 ? T(-(std::numeric_limits<T>::max)()) : (std::numeric_limits<T>::min)();
279 # pragma warning(pop)
280 #elif defined(__BORLANDC__)
283 } // namespace detail
287 template<typename Target, typename Source>
288 inline Target numeric_cast(Source arg BOOST_EXPLICIT_DEFAULT_TARGET)
290 // typedefs abbreviating respective trait classes
291 typedef detail::fixed_numeric_limits<Source> arg_traits;
292 typedef detail::fixed_numeric_limits<Target> result_traits;
294 #if defined(BOOST_STRICT_CONFIG) \
295 || (!defined(__HP_aCC) || __HP_aCC > 33900) \
296 && (!defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) \
297 || defined(BOOST_SGI_CPP_LIMITS))
298 // typedefs that act as compile time assertions
299 // (to be replaced by boost compile time assertions
300 // as and when they become available and are stable)
301 typedef bool argument_must_be_numeric[arg_traits::is_specialized];
302 typedef bool result_must_be_numeric[result_traits::is_specialized];
304 const bool arg_is_signed = arg_traits::is_signed;
305 const bool result_is_signed = result_traits::is_signed;
306 const bool same_sign = arg_is_signed == result_is_signed;
308 if (less_than_type_min<arg_is_signed, result_is_signed>::check(arg, (result_traits::min)())
309 || greater_than_type_max<same_sign, arg_is_signed>::check(arg, (result_traits::max)())
312 #else // We need to use #pragma hacks if available
315 # pragma warning(push)
316 # pragma warning(disable : 4018)
317 #elif defined(__BORLANDC__)
318 #pragma option push -w-8012
320 if ((arg < 0 && !result_traits::is_signed) // loss of negative range
321 || (arg_traits::is_signed && arg < (result_traits::min)()) // underflow
322 || arg > (result_traits::max)()) // overflow
324 # pragma warning(pop)
325 #elif defined(__BORLANDC__)
330 throw bad_numeric_cast();
332 return static_cast<Target>(arg);
335 # undef BOOST_EXPLICIT_DEFAULT_TARGET
339 #endif // BOOST_OLD_NUMERIC_CAST_HPP