mirror of
https://github.com/boostorg/conversion.git
synced 2026-08-06 21:44:08 +02:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e6be985c4e | |||
| beb912a139 | |||
| 4728acd652 | |||
| e3ecdf7a27 | |||
| 02436dd1c6 | |||
| d08d2cb415 | |||
| 7470535162 | |||
| 906d09b511 |
+128
-40
@@ -9,7 +9,11 @@
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
// Revision History
|
||||
// 15 Jul 00 Suppress numeric_cast warnings for GCC, Borland and MSVC (Dave Abrahams)
|
||||
// 29 Nov 00 Remove nested namespace cast, cleanup spacing before Formal
|
||||
// Review (Beman Dawes)
|
||||
// 19 Oct 00 Fix numeric_cast for floating-point types (Dave Abrahams)
|
||||
// 15 Jul 00 Suppress numeric_cast warnings for GCC, Borland and MSVC
|
||||
// (Dave Abrahams)
|
||||
// 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams)
|
||||
// 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes)
|
||||
// 27 Jun 00 More MSVC6 workarounds
|
||||
@@ -48,16 +52,13 @@
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace detail {
|
||||
namespace detail
|
||||
{
|
||||
template <class T> struct type_wrapper {};
|
||||
}
|
||||
#if !(defined(BOOST_MSVC) && BOOST_MSVC <= 1200) // 1200 = VC6
|
||||
namespace cast
|
||||
{
|
||||
#endif
|
||||
|
||||
// See the documentation for descriptions of how to choose between
|
||||
// static_cast<>, dynamic_cast<>, polymorphic_cast<>. and down_cast<>
|
||||
// static_cast<>, dynamic_cast<>, polymorphic_cast<> and polymorphic_downcast<>
|
||||
|
||||
// polymorphic_cast --------------------------------------------------------//
|
||||
|
||||
@@ -116,15 +117,71 @@ namespace boost
|
||||
// numeric_cast ------------------------------------------------------------//
|
||||
|
||||
// Move to config.hpp?
|
||||
#if defined(__SGI_STL_PORT) && __SGI_STL_PORT <= 0x400 && __STL_STATIC_CONST_INIT_BUG
|
||||
#if defined(_RWSTD_VER) || (defined(__SGI_STL_PORT) && __SGI_STL_PORT <= 0x400 && __STL_STATIC_CONST_INIT_BUG)
|
||||
// STLPort 4.0 doesn't define the static constants in numeric_limits<> so that they
|
||||
// can be used at compile time if the compiler bug indicated by
|
||||
// __STL_STATIC_CONST_INIT_BUG is present.
|
||||
|
||||
// Rogue wave STL (C++ Builder) also has broken numeric_limits
|
||||
// with default template defining members out of line.
|
||||
# define BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
||||
// less_than_type_min -
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <bool is_signed> struct numeric_min_select;
|
||||
|
||||
template<>
|
||||
struct numeric_min_select<true>
|
||||
{
|
||||
template <class T>
|
||||
struct limits : std::numeric_limits<T>
|
||||
{
|
||||
static inline T min()
|
||||
# ifndef __GNUC__ // bug workaround courtesy Jens Maurer
|
||||
{
|
||||
return std::numeric_limits<T>::min() >= 0
|
||||
// unary minus causes integral promotion, thus the static_cast<>
|
||||
? static_cast<T>(-std::numeric_limits<T>::max())
|
||||
: std::numeric_limits<T>::min();
|
||||
}
|
||||
# else
|
||||
;
|
||||
# endif
|
||||
};
|
||||
};
|
||||
|
||||
# ifdef __GNUC__ // bug workaround courtesy Jens Maurer
|
||||
template<> template<class T>
|
||||
inline T numeric_min_select<true>::limits<T>::min()
|
||||
{
|
||||
return std::numeric_limits<T>::min() >= 0
|
||||
// unary minus causes integral promotion, thus the static_cast<>
|
||||
? static_cast<T>(-std::numeric_limits<T>::max())
|
||||
: std::numeric_limits<T>::min();
|
||||
}
|
||||
# endif
|
||||
|
||||
template<>
|
||||
struct numeric_min_select<false>
|
||||
{
|
||||
template <class T>
|
||||
struct limits : std::numeric_limits<T> {};
|
||||
};
|
||||
|
||||
// Move to namespace boost in utility.hpp?
|
||||
template <class T>
|
||||
struct fixed_numeric_limits
|
||||
: public numeric_min_select<
|
||||
std::numeric_limits<T>::is_signed
|
||||
>::template limits<T>
|
||||
{
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
// less_than_type_min -
|
||||
// x_is_signed should be numeric_limits<X>::is_signed
|
||||
// y_is_signed should be numeric_limits<Y>::is_signed
|
||||
// y_min should be numeric_limits<Y>::min()
|
||||
@@ -166,26 +223,73 @@ namespace boost
|
||||
// between signed and unsigned values.
|
||||
//
|
||||
// "poor man's partial specialization" is in use here.
|
||||
template <bool same_sign>
|
||||
template <bool same_sign, bool x_is_signed>
|
||||
struct greater_than_type_max;
|
||||
|
||||
template<>
|
||||
struct greater_than_type_max<true>
|
||||
struct greater_than_type_max<true, true>
|
||||
{
|
||||
template <class X, class Y>
|
||||
static bool check(X x, Y y_max)
|
||||
static inline bool check(X x, Y y_max)
|
||||
{ return x > y_max; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct greater_than_type_max<false>
|
||||
struct greater_than_type_max<false, true>
|
||||
{
|
||||
// What does the standard say about this? I think it's right, and it
|
||||
// will work with every compiler I know of.
|
||||
template <class X, class Y>
|
||||
static bool check(X x, Y)
|
||||
static inline bool check(X x, Y)
|
||||
{ return x >= 0 && static_cast<X>(static_cast<Y>(x)) != x; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct greater_than_type_max<true, false>
|
||||
{
|
||||
template <class X, class Y>
|
||||
static inline bool check(X x, Y y_max)
|
||||
{ return x > y_max; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct greater_than_type_max<false, false>
|
||||
{
|
||||
// What does the standard say about this? I think it's right, and it
|
||||
// will work with every compiler I know of.
|
||||
template <class X, class Y>
|
||||
static inline bool check(X x, Y)
|
||||
{ return static_cast<X>(static_cast<Y>(x)) != x; }
|
||||
};
|
||||
|
||||
#else // use #pragma hacks if available
|
||||
|
||||
namespace detail
|
||||
{
|
||||
# if BOOST_MSVC
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable : 4018)
|
||||
# pragma warning(disable : 4146)
|
||||
#elif defined(__BORLANDC__)
|
||||
#pragma option push -w-8041
|
||||
# endif
|
||||
// Move to namespace boost in utility.hpp?
|
||||
template <class T>
|
||||
struct fixed_numeric_limits : public std::numeric_limits<T>
|
||||
{
|
||||
static inline T min()
|
||||
{
|
||||
return std::numeric_limits<T>::is_signed && std::numeric_limits<T>::min() >= 0
|
||||
? T(-std::numeric_limits<T>::max()) : std::numeric_limits<T>::min();
|
||||
}
|
||||
};
|
||||
# if BOOST_MSVC
|
||||
# pragma warning(pop)
|
||||
#elif defined(__BORLANDC__)
|
||||
#pragma option pop
|
||||
# endif
|
||||
} // namespace detail
|
||||
|
||||
#endif
|
||||
|
||||
template<typename Target, typename Source>
|
||||
@@ -193,7 +297,7 @@ namespace boost
|
||||
{
|
||||
// typedefs abbreviating respective trait classes
|
||||
typedef std::numeric_limits<Source> arg_traits;
|
||||
typedef std::numeric_limits<Target> result_traits;
|
||||
typedef detail::fixed_numeric_limits<Target> result_traits;
|
||||
|
||||
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
||||
// typedefs that act as compile time assertions
|
||||
@@ -207,7 +311,7 @@ namespace boost
|
||||
const bool same_sign = arg_is_signed == result_is_signed;
|
||||
|
||||
if (less_than_type_min<arg_is_signed, result_is_signed>::check(arg, result_traits::min())
|
||||
|| greater_than_type_max<same_sign>::check(arg, result_traits::max())
|
||||
|| greater_than_type_max<same_sign, arg_is_signed>::check(arg, result_traits::max())
|
||||
)
|
||||
|
||||
#else // We need to use #pragma hacks if available
|
||||
@@ -215,38 +319,23 @@ namespace boost
|
||||
# if BOOST_MSVC
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable : 4018)
|
||||
#elif defined(__BORLANDC__)
|
||||
#pragma option push -w-8012
|
||||
# endif
|
||||
if ( (arg < 0 && !result_traits::is_signed) || // loss of negative range
|
||||
(arg_traits::is_signed &&
|
||||
arg < result_traits::min()) || // underflow
|
||||
arg > result_traits::max() ) // overflow
|
||||
if ((arg < 0 && !result_traits::is_signed) // loss of negative range
|
||||
|| (arg_traits::is_signed && arg < result_traits::min()) // underflow
|
||||
|| arg > result_traits::max()) // overflow
|
||||
# if BOOST_MSVC
|
||||
# pragma warning(pop)
|
||||
#elif defined(__BORLANDC__)
|
||||
#pragma option pop
|
||||
# endif
|
||||
#endif
|
||||
{
|
||||
throw bad_numeric_cast();
|
||||
}
|
||||
return static_cast<Target>(arg);
|
||||
}
|
||||
|
||||
// Visual C++ workarounds --------------------------------------------------//
|
||||
|
||||
# if !(defined(BOOST_MSVC) && BOOST_MSVC <= 1200) // 1200 = VC6
|
||||
} // namespace cast
|
||||
|
||||
using ::boost::cast::polymorphic_cast;
|
||||
using ::boost::cast::polymorphic_downcast;
|
||||
using ::boost::cast::bad_numeric_cast;
|
||||
using ::boost::cast::numeric_cast;
|
||||
# else
|
||||
namespace cast {
|
||||
using ::boost::polymorphic_cast;
|
||||
using ::boost::polymorphic_downcast;
|
||||
using ::boost::bad_numeric_cast;
|
||||
using ::boost::numeric_cast;
|
||||
}
|
||||
# endif
|
||||
} // numeric_cast
|
||||
|
||||
# undef BOOST_EXPLICIT_DEFAULT_TARGET
|
||||
# undef BOOST_EXPLICIT_TARGET
|
||||
@@ -254,4 +343,3 @@ namespace boost
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_CAST_HPP
|
||||
|
||||
|
||||
Reference in New Issue
Block a user