mirror of
https://github.com/boostorg/conversion.git
synced 2025-08-05 15:34:29 +02:00
Old numeric_cast<> completely replaced by new code (except that the old code is retained as a fallback mechanism for old broken compilers that can't deal with the new code)
[SVN r29761]
This commit is contained in:
98
cast.htm
98
cast.htm
@@ -3,7 +3,7 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
"HTML Tidy for Cygwin (vers 1st April 2002), see www.w3.org">
|
"Microsoft FrontPage 5.0">
|
||||||
<meta http-equiv="Content-Type" content=
|
<meta http-equiv="Content-Type" content=
|
||||||
"text/html; charset=windows-1252">
|
"text/html; charset=windows-1252">
|
||||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||||
@@ -19,10 +19,9 @@
|
|||||||
|
|
||||||
<h2><a name="Cast Functions">Cast Functions</a></h2>
|
<h2><a name="Cast Functions">Cast Functions</a></h2>
|
||||||
|
|
||||||
<p>The header <a href="../../boost/cast.hpp">boost/cast.hpp</a> provides
|
<p>The header <a href="../../boost/cast.hpp">boost/cast.hpp</a> provides <code>
|
||||||
<a href="#Polymorphic_cast"><code>polymorphic_cast</code></a>, <a href=
|
<a href="#Polymorphic_cast">polymorphic_cast</a> and</code> <a href=
|
||||||
"#Polymorphic_cast"><code>polymorphic_downcast</code></a>, and <a href=
|
"#Polymorphic_cast"><code>polymorphic_downcast</code></a> function templates designed to
|
||||||
"#numeric_cast"><code>numeric_cast</code></a> function templates designed to
|
|
||||||
complement the C++ built-in casts.</p>
|
complement the C++ built-in casts.</p>
|
||||||
|
|
||||||
<p>The program <a href="cast_test.cpp">cast_test.cpp</a> can be used to
|
<p>The program <a href="cast_test.cpp">cast_test.cpp</a> can be used to
|
||||||
@@ -62,9 +61,8 @@
|
|||||||
<p>A <code>polymorphic_downcast</code> should be used for
|
<p>A <code>polymorphic_downcast</code> should be used for
|
||||||
downcasts that you are certain should succeed. Error checking is
|
downcasts that you are certain should succeed. Error checking is
|
||||||
only performed in translation units where <code>NDEBUG</code> is
|
only performed in translation units where <code>NDEBUG</code> is
|
||||||
not defined, via
|
not defined, via
|
||||||
<pre>
|
<pre> assert( dynamic_cast<Derived>(x) == x )
|
||||||
assert( dynamic_cast<Derived>(x) == x )
|
|
||||||
</pre> where <code>x</code> is the source pointer. This approach
|
</pre> where <code>x</code> is the source pointer. This approach
|
||||||
ensures that not only is a non-zero pointer returned, but also
|
ensures that not only is a non-zero pointer returned, but also
|
||||||
that it is correct in the presence of multiple inheritance.
|
that it is correct in the presence of multiple inheritance.
|
||||||
@@ -86,8 +84,7 @@
|
|||||||
<h3>polymorphic_cast and polymorphic_downcast synopsis</h3>
|
<h3>polymorphic_cast and polymorphic_downcast synopsis</h3>
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>namespace boost {
|
||||||
namespace boost {
|
|
||||||
|
|
||||||
template <class Derived, class Base>
|
template <class Derived, class Base>
|
||||||
inline Derived polymorphic_cast(Base* x);
|
inline Derived polymorphic_cast(Base* x);
|
||||||
@@ -106,8 +103,7 @@ inline Derived polymorphic_downcast(Base* x);
|
|||||||
<h3>polymorphic_downcast example</h3>
|
<h3>polymorphic_downcast example</h3>
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>
|
<pre>#include <boost/cast.hpp>
|
||||||
#include <boost/cast.hpp>
|
|
||||||
...
|
...
|
||||||
class Fruit { public: virtual ~Fruit(){}; ... };
|
class Fruit { public: virtual ~Fruit(){}; ... };
|
||||||
class Banana : public Fruit { ... };
|
class Banana : public Fruit { ... };
|
||||||
@@ -119,85 +115,20 @@ void f( Fruit * fruit ) {
|
|||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
<h3><a name="numeric_cast">numeric_cast</a></h3>
|
|
||||||
|
|
||||||
<p>A <code>static_cast</code> or implicit conversion will not detect failure to
|
|
||||||
preserve range for numeric casts. The <code>numeric_cast</code> function
|
|
||||||
templates are similar to <code>static_cast</code> and certain (dubious)
|
|
||||||
implicit conversions in this respect, except that they detect loss of
|
|
||||||
numeric range. An exception is thrown when a runtime value-preservation
|
|
||||||
check fails.</p>
|
|
||||||
|
|
||||||
<p>The requirements on the argument and result types are:</p>
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<ul>
|
|
||||||
<li>Both argument and result types are CopyConstructible [ISO Std
|
|
||||||
20.1.3].</li>
|
|
||||||
|
|
||||||
<li>Both argument and result types are Numeric, defined by
|
|
||||||
<code>std::numeric_limits<>::is_specialized</code> being
|
|
||||||
true.</li>
|
|
||||||
|
|
||||||
<li>The argument can be converted to the result type using
|
|
||||||
<code>static_cast</code>.</li>
|
|
||||||
</ul>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<h3>numeric_cast synopsis</h3>
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
namespace boost {
|
|
||||||
|
|
||||||
class bad_numeric_cast : public std::bad_cast {...};
|
|
||||||
|
|
||||||
template<typename Target, typename Source>
|
|
||||||
inline Target numeric_cast(Source arg);
|
|
||||||
// Throws: bad_numeric_cast unless, in converting arg from Source to Target,
|
|
||||||
// there is no loss of negative range, and no underflow, and no
|
|
||||||
// overflow, as determined by std::numeric_limits
|
|
||||||
// Returns: static_cast<Target>(arg)
|
|
||||||
|
|
||||||
}
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<h3>numeric_cast example</h3>
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
#include <boost/cast.hpp>
|
|
||||||
using namespace boost::cast;
|
|
||||||
|
|
||||||
void ariane(double vx)
|
|
||||||
{
|
|
||||||
...
|
|
||||||
unsigned short dx = numeric_cast<unsigned short>(vx);
|
|
||||||
...
|
|
||||||
}
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<h3>numeric_cast rationale</h3>
|
|
||||||
|
|
||||||
<p>The form of the throws condition is specified so that != is not a
|
|
||||||
required operation.</p>
|
|
||||||
|
|
||||||
<h3>History</h3>
|
<h3>History</h3>
|
||||||
|
|
||||||
<p><code>polymorphic_cast</code> was suggested by Bjarne Stroustrup in "The C++
|
<p><code>polymorphic_cast</code> was suggested by Bjarne Stroustrup in "The C++
|
||||||
Programming Language".<br>
|
Programming Language".<br>
|
||||||
<code>polymorphic_downcast</code> was contributed by <a href=
|
<code>polymorphic_downcast</code> was contributed by <a href=
|
||||||
"../../people/dave_abrahams.htm">Dave Abrahams</a>.<code><br>
|
"../../people/dave_abrahams.htm">Dave Abrahams</a>.<code><br>
|
||||||
numeric_cast</code> was contributed by <a href=
|
An old
|
||||||
"../../people/kevlin_henney.htm">Kevlin Henney</a>.</p>
|
numeric_cast</code> that was contributed by <a href=
|
||||||
|
"../../people/kevlin_henney.htm">Kevlin Henney</a> is now superseeded by the <a href="../numeric/conversion/doc/index.html">Boost Numeric Conversion Library</a></p>
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
<p>Revised
|
<p>Revised
|
||||||
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan
|
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan
|
||||||
-->06 January, 2001
|
-->June 23, 2005<!--webbot bot="Timestamp" endspan i-checksum="30348"
|
||||||
<!--webbot bot="Timestamp" endspan i-checksum="38320"
|
|
||||||
--></p>
|
--></p>
|
||||||
|
|
||||||
<p>© Copyright boost.org 1999. Permission to copy, use, modify, sell
|
<p>© Copyright boost.org 1999. Permission to copy, use, modify, sell
|
||||||
@@ -206,5 +137,4 @@ void ariane(double vx)
|
|||||||
or implied warranty, and with no claim as to its suitability for any
|
or implied warranty, and with no claim as to its suitability for any
|
||||||
purpose.</p>
|
purpose.</p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
@@ -1,6 +1,6 @@
|
|||||||
// boost cast.hpp header file ----------------------------------------------//
|
// boost cast.hpp header file ----------------------------------------------//
|
||||||
|
|
||||||
// (C) Copyright Kevlin Henney and Dave Abrahams 1999.
|
// (C) Copyright Kevlin Henney and Dave Abrahams 1999.
|
||||||
// Distributed under the Boost
|
// Distributed under the Boost
|
||||||
// Software License, Version 1.0. (See accompanying file
|
// Software License, Version 1.0. (See accompanying file
|
||||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
@@ -8,9 +8,10 @@
|
|||||||
// See http://www.boost.org/libs/conversion for Documentation.
|
// See http://www.boost.org/libs/conversion for Documentation.
|
||||||
|
|
||||||
// Revision History
|
// Revision History
|
||||||
// 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
|
// 23 JUn 05 numeric_cast removed and redirected to the new verion (Fernando Cacciola)
|
||||||
// <boost/limits.hpp> instead (the workaround did not
|
// 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
|
||||||
// actually compile when BOOST_NO_LIMITS was defined in
|
// <boost/limits.hpp> instead (the workaround did not
|
||||||
|
// actually compile when BOOST_NO_LIMITS was defined in
|
||||||
// any case, so we loose nothing). (John Maddock)
|
// any case, so we loose nothing). (John Maddock)
|
||||||
// 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never
|
// 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never
|
||||||
// worked with stock GCC; trying to get it to do that broke
|
// worked with stock GCC; trying to get it to do that broke
|
||||||
@@ -26,14 +27,14 @@
|
|||||||
// (Dave Abrahams)
|
// (Dave Abrahams)
|
||||||
// 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams)
|
// 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams)
|
||||||
// 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes)
|
// 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes)
|
||||||
// 27 Jun 00 More MSVC6 workarounds
|
// 27 Jun 00 More MSVC6 workarounds
|
||||||
// 15 Jun 00 Add workarounds for MSVC6
|
// 15 Jun 00 Add workarounds for MSVC6
|
||||||
// 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov)
|
// 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov)
|
||||||
// 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar)
|
// 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar)
|
||||||
// 29 Dec 99 Change using declarations so usages in other namespaces work
|
// 29 Dec 99 Change using declarations so usages in other namespaces work
|
||||||
// correctly (Dave Abrahams)
|
// correctly (Dave Abrahams)
|
||||||
// 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors
|
// 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors
|
||||||
// as suggested Darin Adler and improved by Valentin Bonnard.
|
// as suggested Darin Adler and improved by Valentin Bonnard.
|
||||||
// 2 Sep 99 Remove controversial asserts, simplify, rename.
|
// 2 Sep 99 Remove controversial asserts, simplify, rename.
|
||||||
// 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast,
|
// 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast,
|
||||||
// place in nested namespace.
|
// place in nested namespace.
|
||||||
@@ -68,7 +69,7 @@ namespace boost
|
|||||||
// polymorphic_cast --------------------------------------------------------//
|
// polymorphic_cast --------------------------------------------------------//
|
||||||
|
|
||||||
// Runtime checked polymorphic downcasts and crosscasts.
|
// Runtime checked polymorphic downcasts and crosscasts.
|
||||||
// Suggested in The C++ Programming Language, 3rd Ed, Bjarne Stroustrup,
|
// Suggested in The C++ Programming Language, 3rd Ed, Bjarne Stroustrup,
|
||||||
// section 15.8 exercise 1, page 425.
|
// section 15.8 exercise 1, page 425.
|
||||||
|
|
||||||
template <class Target, class Source>
|
template <class Target, class Source>
|
||||||
@@ -98,284 +99,9 @@ namespace boost
|
|||||||
// implicit_cast -----------------------------------------------------------//
|
// implicit_cast -----------------------------------------------------------//
|
||||||
//
|
//
|
||||||
// Removed due to uncertain purpose. Use either numeric_cast (see below)
|
// Removed due to uncertain purpose. Use either numeric_cast (see below)
|
||||||
// or static_cast according to the need.
|
// or static_cast according to the need.
|
||||||
|
|
||||||
// numeric_cast and related exception --------------------------------------//
|
#include <boost/numeric/conversion/cast.hpp>
|
||||||
|
|
||||||
// Contributed by Kevlin Henney
|
|
||||||
|
|
||||||
// bad_numeric_cast --------------------------------------------------------//
|
|
||||||
|
|
||||||
// exception used to indicate runtime numeric_cast failure
|
|
||||||
class bad_numeric_cast : public std::bad_cast
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// constructors, destructors and assignment operator defaulted
|
|
||||||
|
|
||||||
// function inlined for brevity and consistency with rest of library
|
|
||||||
virtual const char *what() const throw()
|
|
||||||
{
|
|
||||||
return "bad numeric cast: loss of range in numeric_cast";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// numeric_cast ------------------------------------------------------------//
|
|
||||||
|
|
||||||
#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(BOOST_SGI_CPP_LIMITS)
|
|
||||||
|
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
template <class T>
|
|
||||||
struct signed_numeric_limits : std::numeric_limits<T>
|
|
||||||
{
|
|
||||||
static inline T min BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
|
||||||
{
|
|
||||||
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)();
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// Move to namespace boost in utility.hpp?
|
|
||||||
template <class T, bool specialized>
|
|
||||||
struct fixed_numeric_limits_base
|
|
||||||
: public if_true< std::numeric_limits<T>::is_signed >
|
|
||||||
::BOOST_NESTED_TEMPLATE then< signed_numeric_limits<T>,
|
|
||||||
std::numeric_limits<T>
|
|
||||||
>::type
|
|
||||||
{};
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
struct fixed_numeric_limits
|
|
||||||
: fixed_numeric_limits_base<T,(std::numeric_limits<T>::is_specialized)>
|
|
||||||
{};
|
|
||||||
|
|
||||||
# ifdef BOOST_HAS_LONG_LONG
|
|
||||||
// cover implementations which supply no specialization for long
|
|
||||||
// long / unsigned long long. Not intended to be full
|
|
||||||
// numeric_limits replacements, but good enough for numeric_cast<>
|
|
||||||
template <>
|
|
||||||
struct fixed_numeric_limits_base< ::boost::long_long_type, false>
|
|
||||||
{
|
|
||||||
BOOST_STATIC_CONSTANT(bool, is_specialized = true);
|
|
||||||
BOOST_STATIC_CONSTANT(bool, is_signed = true);
|
|
||||||
static ::boost::long_long_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
|
||||||
{
|
|
||||||
# ifdef LONGLONG_MAX
|
|
||||||
return LONGLONG_MAX;
|
|
||||||
# else
|
|
||||||
return 9223372036854775807LL; // hope this is portable
|
|
||||||
# endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static ::boost::long_long_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
|
||||||
{
|
|
||||||
# ifdef LONGLONG_MIN
|
|
||||||
return LONGLONG_MIN;
|
|
||||||
# else
|
|
||||||
return -( 9223372036854775807LL )-1; // hope this is portable
|
|
||||||
# endif
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct fixed_numeric_limits_base< ::boost::ulong_long_type, false>
|
|
||||||
{
|
|
||||||
BOOST_STATIC_CONSTANT(bool, is_specialized = true);
|
|
||||||
BOOST_STATIC_CONSTANT(bool, is_signed = false);
|
|
||||||
static ::boost::ulong_long_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
|
||||||
{
|
|
||||||
# ifdef ULONGLONG_MAX
|
|
||||||
return ULONGLONG_MAX;
|
|
||||||
# else
|
|
||||||
return 0xffffffffffffffffULL; // hope this is portable
|
|
||||||
# endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static ::boost::ulong_long_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
|
|
||||||
};
|
|
||||||
# endif
|
|
||||||
} // 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()
|
|
||||||
//
|
|
||||||
// check(x, y_min) returns true iff x < y_min without invoking comparisons
|
|
||||||
// between signed and unsigned values.
|
|
||||||
//
|
|
||||||
// "poor man's partial specialization" is in use here.
|
|
||||||
template <bool x_is_signed, bool y_is_signed>
|
|
||||||
struct less_than_type_min
|
|
||||||
{
|
|
||||||
template <class X, class Y>
|
|
||||||
static bool check(X x, Y y_min)
|
|
||||||
{ return x < y_min; }
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct less_than_type_min<false, true>
|
|
||||||
{
|
|
||||||
template <class X, class Y>
|
|
||||||
static bool check(X, Y)
|
|
||||||
{ return false; }
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct less_than_type_min<true, false>
|
|
||||||
{
|
|
||||||
template <class X, class Y>
|
|
||||||
static bool check(X x, Y)
|
|
||||||
{ return x < 0; }
|
|
||||||
};
|
|
||||||
|
|
||||||
// greater_than_type_max -
|
|
||||||
// same_sign should be:
|
|
||||||
// numeric_limits<X>::is_signed == numeric_limits<Y>::is_signed
|
|
||||||
// y_max should be numeric_limits<Y>::max()
|
|
||||||
//
|
|
||||||
// check(x, y_max) returns true iff x > y_max without invoking comparisons
|
|
||||||
// between signed and unsigned values.
|
|
||||||
//
|
|
||||||
// "poor man's partial specialization" is in use here.
|
|
||||||
template <bool same_sign, bool x_is_signed>
|
|
||||||
struct greater_than_type_max;
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct greater_than_type_max<true, true>
|
|
||||||
{
|
|
||||||
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, 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 inline bool check(X x, Y)
|
|
||||||
{ return x >= 0 && static_cast<X>(static_cast<Y>(x)) != x; }
|
|
||||||
|
|
||||||
# if defined(BOOST_MSVC) && BOOST_MSVC <= 1200
|
|
||||||
// MSVC6 can't static_cast unsigned __int64 -> floating types
|
|
||||||
# define BOOST_UINT64_CAST(src_type) \
|
|
||||||
static inline bool check(src_type x, unsigned __int64) \
|
|
||||||
{ \
|
|
||||||
if (x < 0) return false; \
|
|
||||||
unsigned __int64 y = static_cast<unsigned __int64>(x); \
|
|
||||||
bool odd = y & 0x1; \
|
|
||||||
__int64 div2 = static_cast<__int64>(y >> 1); \
|
|
||||||
return ((static_cast<src_type>(div2) * 2.0) + odd) != x; \
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_UINT64_CAST(long double);
|
|
||||||
BOOST_UINT64_CAST(double);
|
|
||||||
BOOST_UINT64_CAST(float);
|
|
||||||
# undef BOOST_UINT64_CAST
|
|
||||||
# endif
|
|
||||||
};
|
|
||||||
|
|
||||||
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 BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
|
||||||
{
|
|
||||||
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>
|
|
||||||
inline Target numeric_cast(Source arg BOOST_EXPLICIT_DEFAULT_TARGET)
|
|
||||||
{
|
|
||||||
// typedefs abbreviating respective trait classes
|
|
||||||
typedef detail::fixed_numeric_limits<Source> arg_traits;
|
|
||||||
typedef detail::fixed_numeric_limits<Target> result_traits;
|
|
||||||
|
|
||||||
#if defined(BOOST_STRICT_CONFIG) \
|
|
||||||
|| (!defined(__HP_aCC) || __HP_aCC > 33900) \
|
|
||||||
&& (!defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) \
|
|
||||||
|| defined(BOOST_SGI_CPP_LIMITS))
|
|
||||||
// typedefs that act as compile time assertions
|
|
||||||
// (to be replaced by boost compile time assertions
|
|
||||||
// as and when they become available and are stable)
|
|
||||||
typedef bool argument_must_be_numeric[arg_traits::is_specialized];
|
|
||||||
typedef bool result_must_be_numeric[result_traits::is_specialized];
|
|
||||||
|
|
||||||
const bool arg_is_signed = arg_traits::is_signed;
|
|
||||||
const bool result_is_signed = result_traits::is_signed;
|
|
||||||
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, arg_is_signed>::check(arg, (result_traits::max)())
|
|
||||||
)
|
|
||||||
|
|
||||||
#else // We need to use #pragma hacks if available
|
|
||||||
|
|
||||||
# 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 BOOST_MSVC
|
|
||||||
# pragma warning(pop)
|
|
||||||
#elif defined(__BORLANDC__)
|
|
||||||
#pragma option pop
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
throw bad_numeric_cast();
|
|
||||||
}
|
|
||||||
return static_cast<Target>(arg);
|
|
||||||
} // numeric_cast
|
|
||||||
|
|
||||||
# undef BOOST_EXPLICIT_DEFAULT_TARGET
|
# undef BOOST_EXPLICIT_DEFAULT_TARGET
|
||||||
|
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Language" content="en-us">
|
<meta http-equiv="Content-Language" content="en-us">
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
||||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
|
||||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||||
<title>Boost Conversion Library</title>
|
<title>Boost Conversion Library</title>
|
||||||
</head>
|
</head>
|
||||||
@@ -22,8 +22,7 @@ supplied by several headers:</p>
|
|||||||
<ul>
|
<ul>
|
||||||
<li>The <a href="cast.htm">boost/cast</a> header provides <b>polymorphic_cast<></b>
|
<li>The <a href="cast.htm">boost/cast</a> header provides <b>polymorphic_cast<></b>
|
||||||
and <b>polymorphic_downcast<></b> to perform safe casting between
|
and <b>polymorphic_downcast<></b> to perform safe casting between
|
||||||
polymorphic types, and <b> numeric_cast</b><i><></i> to perform safe casting
|
polymorphic types.<br>
|
||||||
between numeric types.<br>
|
|
||||||
</li>
|
</li>
|
||||||
<li>The <a href="lexical_cast.htm">boost/lexical_cast</a> header provides <b>lexical_cast<></b>
|
<li>The <a href="lexical_cast.htm">boost/lexical_cast</a> header provides <b>lexical_cast<></b>
|
||||||
general literal text conversions, such as an <code>int</code> represented as
|
general literal text conversions, such as an <code>int</code> represented as
|
||||||
@@ -31,9 +30,9 @@ supplied by several headers:</p>
|
|||||||
</ul>
|
</ul>
|
||||||
<hr>
|
<hr>
|
||||||
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED"
|
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED"
|
||||||
S-Format="%d %B, %Y" startspan -->06 January, 2001<!--webbot bot="Timestamp" endspan i-checksum="38320" -->
|
S-Format="%d %B, %Y" startspan -->June 23, 2005<!--webbot bot="Timestamp" endspan i-checksum="30348" -->
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
@@ -27,7 +27,6 @@ DEPENDS all : test ;
|
|||||||
: [ run implicit_cast.cpp ]
|
: [ run implicit_cast.cpp ]
|
||||||
[ compile-fail implicit_cast_fail.cpp ]
|
[ compile-fail implicit_cast_fail.cpp ]
|
||||||
[ run ../cast_test.cpp ]
|
[ run ../cast_test.cpp ]
|
||||||
[ run ../numeric_cast_test.cpp ]
|
|
||||||
[ run ../lexical_cast_test.cpp ]
|
[ run ../lexical_cast_test.cpp ]
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user