diff --git a/cast.htm b/cast.htm index a7e55f2..587d08a 100644 --- a/cast.htm +++ b/cast.htm @@ -3,7 +3,7 @@
+ "Microsoft FrontPage 5.0"> @@ -19,10 +19,9 @@The header boost/cast.hpp provides
- polymorphic_cast
, polymorphic_downcast
, and numeric_cast
function templates designed to
+
The header boost/cast.hpp provides
+ polymorphic_cast and
polymorphic_downcast
function templates designed to
complement the C++ built-in casts.
The program cast_test.cpp can be used to @@ -62,9 +61,8 @@
A polymorphic_downcast
should be used for
downcasts that you are certain should succeed. Error checking is
only performed in translation units where NDEBUG
is
- not defined, via
-
- assert( dynamic_cast<Derived>(x) == x ) + not defined, via +assert( dynamic_cast<Derived>(x) == x )wherex
is the source pointer. This approach ensures that not only is a non-zero pointer returned, but also that it is correct in the presence of multiple inheritance. @@ -86,8 +84,7 @@polymorphic_cast and polymorphic_downcast synopsis
--namespace boost { +namespace boost { template <class Derived, class Base> inline Derived polymorphic_cast(Base* x); @@ -106,8 +103,7 @@ inline Derived polymorphic_downcast(Base* x);polymorphic_downcast example
---#include <boost/cast.hpp> +#include <boost/cast.hpp> ... class Fruit { public: virtual ~Fruit(){}; ... }; class Banana : public Fruit { ... }; @@ -119,85 +115,20 @@ void f( Fruit * fruit ) {numeric_cast
- -A
- -static_cast
or implicit conversion will not detect failure to - preserve range for numeric casts. Thenumeric_cast
function - templates are similar tostatic_cast
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.The requirements on the argument and result types are:
- --- --
-- Both argument and result types are CopyConstructible [ISO Std - 20.1.3].
- -- Both argument and result types are Numeric, defined by -
- -std::numeric_limits<>::is_specialized
being - true.- The argument can be converted to the result type using -
-static_cast
.numeric_cast synopsis
- --- --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) - -} --numeric_cast example
- --- --#include <boost/cast.hpp> -using namespace boost::cast; - -void ariane(double vx) -{ - ... - unsigned short dx = numeric_cast<unsigned short>(vx); - ... -} --numeric_cast rationale
- -The form of the throws condition is specified so that != is not a - required operation.
-History
+ An old + numeric_cast that was contributed by Kevlin Henney is now superseeded by the Boost Numeric Conversion Library
polymorphic_cast
was suggested by Bjarne Stroustrup in "The C++ Programming Language".
polymorphic_downcast
was contributed by Dave Abrahams.was contributed by Kevlin Henney.
- numeric_cast
-Revised +
Revised 06 January, 2001 - June 23, 2005
© 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 purpose.
- - +