Compare commits

..

6 Commits

Author SHA1 Message Date
nobody bdd0b39750 This commit was manufactured by cvs2svn to create tag
'Version_1_18_2'.

[SVN r8120]
2000-11-03 20:02:18 +00:00
Dave Abrahams d08d2cb415 Fix for fixed_numeric_limits<>::min() in the BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS case.
[SVN r8036]
2000-10-27 13:43:33 +00:00
Dave Abrahams 7470535162 Fix numeric_cast<> bugs with floating types.
[SVN r8008]
2000-10-19 19:14:03 +00:00
Dave Abrahams 906d09b511 a fix for "unsigned type always passes this test" warnings under GCC.
[SVN r7999]
2000-10-18 13:52:09 +00:00
Dave Abrahams 15539c72c8 Suppress numeric_cast warnings for GCC, Borland and MSVC
[SVN r7626]
2000-07-17 22:33:43 +00:00
Beman Dawes fca67c7788 This commit was generated by cvs2svn to compensate for changes in r4,
which included commits to RCS files with non-trunk default branches.


[SVN r7621]
2000-07-07 16:04:40 +00:00
+90 -13
View File
@@ -9,6 +9,7 @@
// See http://www.boost.org for most recent version including documentation.
// Revision History
// 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)
@@ -124,7 +125,43 @@ namespace boost
#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()
{
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();
}
};
};
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>::limits<T>
{
};
}
// 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 +203,68 @@ 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)
# 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)
# endif
}
#endif
template<typename Target, typename Source>
@@ -193,7 +272,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 +286,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
@@ -216,10 +295,9 @@ namespace boost
# pragma warning(push)
# pragma warning(disable : 4018)
# 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)
# endif
@@ -254,4 +332,3 @@ namespace boost
} // namespace boost
#endif // BOOST_CAST_HPP