Pointers to polymorphic objects (objects of classes which define at
- least one virtual function) are sometimes downcast or crosscast.
- Downcasting means casting from a base class to a derived class.
- Crosscasting means casting across an inheritance hierarchy diagram, such
- as from one base to the other in a Y diagram hierarchy.
-
-
Such casts can be done with old-style casts, but this approach is
- never to be recommended. Old-style casts are sorely lacking in type
- safety, suffer poor readability, and are difficult to locate with search
- tools.
-
-
The C++ built-in static_cast can be used for efficiently
- downcasting pointers to polymorphic objects, but provides no error
- detection for the case where the pointer being cast actually points to
- the wrong derived class. The polymorphic_downcast template retains
- the efficiency of static_cast for non-debug compilations, but for
- debug compilations adds safety via an assert() that a dynamic_cast
- succeeds.
-
-
The C++ built-in dynamic_cast can be used for downcasts and
- crosscasts of pointers to polymorphic objects, but error notification in
- the form of a returned value of 0 is inconvenient to test, or worse yet,
- easy to forget to test. The throwing form of dynamic_cast, which
- works on references, can be used on pointers through the ugly expression
- &dynamic_cast<T&>(*p), which causes undefined
- behavior if p is 0. The polymorphic_cast
- template performs a dynamic_cast on a pointer, and throws an
- exception if the dynamic_cast returns 0.
-
-
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 )
-
where x 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.
- Attempts to crosscast using polymorphic_downcast will
- fail to compile.
- Warning: Because polymorphic_downcast uses assert(), it
- violates the One Definition Rule (ODR) if NDEBUG is inconsistently
- defined across translation units. [See ISO Std 3.2]
-
- For crosscasts, or when the success of a cast can only be known at
- runtime, or when efficiency is not important,
- polymorphic_cast is preferred.
-
-
The C++ built-in dynamic_cast must be used to cast references
- rather than pointers. It is also the only cast that can be used to check
- whether a given interface is supported; in that case a return of 0 isn't
- an error condition.
-
-
polymorphic_cast and polymorphic_downcast synopsis
#include <boost/cast.hpp>
-...
-class Fruit { public: virtual ~Fruit(){}; ... };
-class Banana : public Fruit { ... };
-...
-void f( Fruit * fruit ) {
-// ... logic which leads us to believe it is a Banana
- Banana * banana = boost::polymorphic_downcast<Banana*>(fruit);
- ...
-
-
-
-
History
-
-
polymorphic_cast was suggested by Bjarne Stroustrup in "The C++
- Programming Language".
- polymorphic_downcast was contributed by Dave Abrahams.
- An old
- numeric_cast that was contributed by Kevlin Henney is now superseeded by the Boost Numeric Conversion Library
-
-
\ No newline at end of file
diff --git a/cast_test.cpp b/cast_test.cpp
deleted file mode 100644
index ad6b18f..0000000
--- a/cast_test.cpp
+++ /dev/null
@@ -1,91 +0,0 @@
-// boost utility cast test program -----------------------------------------//
-
-// (C) Copyright Beman Dawes, Dave Abrahams 1999. Distributed under the Boost
-// Software License, Version 1.0. (See accompanying file
-// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
-
-// See http://www.boost.org for most recent version including documentation.
-
-// Revision History
-// 28 Set 04 factored out numeric_cast<> test (Fernando Cacciola)
-// 20 Jan 01 removed use of for portability to raw GCC (David Abrahams)
-// 28 Jun 00 implicit_cast removed (Beman Dawes)
-// 30 Aug 99 value_cast replaced by numeric_cast
-// 3 Aug 99 Initial Version
-
-#include
-#include
-#include // for DBL_MAX (Peter Schmid)
-#include
-
-# if SCHAR_MAX == LONG_MAX
-# error "This test program doesn't work if SCHAR_MAX == LONG_MAX"
-# endif
-
-using namespace boost;
-using std::cout;
-
-namespace
-{
- struct Base
- {
- virtual char kind() { return 'B'; }
- };
-
- struct Base2
- {
- virtual char kind2() { return '2'; }
- };
-
- struct Derived : public Base, Base2
- {
- virtual char kind() { return 'D'; }
- };
-}
-
-
-int main( int argc, char * argv[] )
-{
-# ifdef NDEBUG
- cout << "NDEBUG is defined\n";
-# else
- cout << "NDEBUG is not defined\n";
-# endif
-
- cout << "\nBeginning tests...\n";
-
-// test polymorphic_cast ---------------------------------------------------//
-
- // tests which should succeed
- Base * base = new Derived;
- Base2 * base2 = 0;
- Derived * derived = 0;
- derived = polymorphic_downcast( base ); // downcast
- assert( derived->kind() == 'D' );
-
- derived = 0;
- derived = polymorphic_cast( base ); // downcast, throw on error
- assert( derived->kind() == 'D' );
-
- base2 = polymorphic_cast( base ); // crosscast
- assert( base2->kind2() == '2' );
-
- // tests which should result in errors being detected
- int err_count = 0;
- base = new Base;
-
- if ( argc > 1 && *argv[1] == '1' )
- { derived = polymorphic_downcast( base ); } // #1 assert failure
-
- bool caught_exception = false;
- try { derived = polymorphic_cast( base ); }
- catch (std::bad_cast)
- { cout<<"caught bad_cast\n"; caught_exception = true; }
- if ( !caught_exception ) ++err_count;
- // the following is just so generated code can be inspected
- if ( derived->kind() == 'B' ) ++err_count;
-
- cout << err_count << " errors detected\nTest "
- << (err_count==0 ? "passed\n" : "failed\n");
- return err_count;
-} // main
diff --git a/include/boost/cast.hpp b/include/boost/cast.hpp
deleted file mode 100644
index 2615d18..0000000
--- a/include/boost/cast.hpp
+++ /dev/null
@@ -1,107 +0,0 @@
-// boost cast.hpp header file ----------------------------------------------//
-
-// (C) Copyright Kevlin Henney and Dave Abrahams 1999.
-// Distributed under the Boost
-// Software License, Version 1.0. (See accompanying file
-// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
-
-// See http://www.boost.org/libs/conversion for Documentation.
-
-// Revision History
-// 23 JUn 05 numeric_cast removed and redirected to the new verion (Fernando Cacciola)
-// 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
-// instead (the workaround did not
-// actually compile when BOOST_NO_LIMITS was defined in
-// any case, so we loose nothing). (John Maddock)
-// 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never
-// worked with stock GCC; trying to get it to do that broke
-// vc-stlport.
-// 20 Jan 01 Moved BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS to config.hpp.
-// Removed unused BOOST_EXPLICIT_TARGET macro. Moved
-// boost::detail::type to boost/type.hpp. Made it compile with
-// stock gcc again (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
-// 15 Jun 00 Add workarounds for MSVC6
-// 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)
-// 29 Dec 99 Change using declarations so usages in other namespaces work
-// correctly (Dave Abrahams)
-// 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors
-// as suggested Darin Adler and improved by Valentin Bonnard.
-// 2 Sep 99 Remove controversial asserts, simplify, rename.
-// 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast,
-// place in nested namespace.
-// 3 Aug 99 Initial version
-
-#ifndef BOOST_CAST_HPP
-#define BOOST_CAST_HPP
-
-# include
-# include
-# include
-# include
-# include
-# include
-
-// It has been demonstrated numerous times that MSVC 6.0 fails silently at link
-// time if you use a template function which has template parameters that don't
-// appear in the function's argument list.
-//
-// TODO: Add this to config.hpp?
-# if defined(BOOST_MSVC) && BOOST_MSVC < 1300
-# define BOOST_EXPLICIT_DEFAULT_TARGET , ::boost::type* = 0
-# else
-# define BOOST_EXPLICIT_DEFAULT_TARGET
-# endif
-
-namespace boost
-{
-// See the documentation for descriptions of how to choose between
-// static_cast<>, dynamic_cast<>, polymorphic_cast<> and polymorphic_downcast<>
-
-// polymorphic_cast --------------------------------------------------------//
-
- // Runtime checked polymorphic downcasts and crosscasts.
- // Suggested in The C++ Programming Language, 3rd Ed, Bjarne Stroustrup,
- // section 15.8 exercise 1, page 425.
-
- template
- inline Target polymorphic_cast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET)
- {
- Target tmp = dynamic_cast(x);
- if ( tmp == 0 ) throw std::bad_cast();
- return tmp;
- }
-
-// polymorphic_downcast ----------------------------------------------------//
-
- // BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited.
-
- // WARNING: Because this cast uses BOOST_ASSERT(), it violates
- // the One Definition Rule if used in multiple translation units
- // where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
- // NDEBUG are defined inconsistently.
-
- // Contributed by Dave Abrahams
-
- template
- inline Target polymorphic_downcast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET)
- {
- BOOST_ASSERT( dynamic_cast(x) == x ); // detect logic error
- return static_cast(x);
- }
-
-# undef BOOST_EXPLICIT_DEFAULT_TARGET
-
-} // namespace boost
-
-# include
-
-#endif // BOOST_CAST_HPP
diff --git a/index.html b/index.html
deleted file mode 100644
index cf8527f..0000000
--- a/index.html
+++ /dev/null
@@ -1,38 +0,0 @@
-
-
-
-
-
-
-
-Boost Conversion Library
-
-
-
-
-
Boost
-Conversion Library
-
-
The Conversion Library improves program safety and clarity by performing
-otherwise messy conversions. It includes cast-style function templates designed to complement the C++
-Standard's built-in casts.
-
To reduce coupling, particularly to standard library IOStreams, the Boost
-Conversion Library is
-supplied by several headers:
-
-
The boost/cast header provides polymorphic_cast<>
- and polymorphic_downcast<> to perform safe casting between
- polymorphic types.
-
-
The boost/lexical_cast header provides lexical_cast<>
- general literal text conversions, such as an int represented as
- a string, or vice-versa.
-
-
-
Revised June 23, 2005
-
-
-
-
-
\ No newline at end of file
diff --git a/lexical_cast.htm b/lexical_cast.htm
deleted file mode 100644
index 949b70c..0000000
--- a/lexical_cast.htm
+++ /dev/null
@@ -1,234 +0,0 @@
-
-
-
-
- lexical_cast
-
-
-
-
-
- Sometimes a value must be converted to a literal text form, such as an int
- represented as a string, or vice-versa, when a string
- is interpreted as an int. Such examples are common when converting
- between data types internal to a program and representation external to a
- program, such as windows and configuration files.
-
- The standard C and C++ libraries offer a number of facilities for performing
- such conversions. However, they vary with their ease of use, extensibility, and
- safety.
-
- For instance, there are a number of limitations with the family of standard C
- functions typified by atoi:
-
-
- Conversion is supported in one direction only: from text to internal data type.
- Converting the other way using the C library requires either the inconvenience
- and compromised safety of the sprintf function, or the loss of
- portability associated with non-standard functions such as itoa.
-
-
- The range of types supported is only a subset of the built-in numeric types,
- namely int, long, and double.
-
-
- The range of types cannot be extended in a uniform manner. For instance,
- conversion from string representation to complex or rational.
-
-
- The standard C functions typified by strtol have the same basic
- limitations, but offer finer control over the conversion process. However, for
- the common case such control is often either not required or not used. The scanf
- family of functions offer even greater control, but also lack safety and ease
- of use.
-
- The standard C++ library offers stringstream for the kind of
- in-core formatting being discussed. It offers a great deal of control over the
- formatting and conversion of I/O to and from arbitrary types through text.
- However, for simple conversions direct use of stringstream can be
- either clumsy (with the introduction of extra local variables and the loss of
- infix-expression convenience) or obscure (where stringstream
- objects are created as temporary objects in an expression). Facets provide a
- comprehensive concept and facility for controlling textual representation, but
- their perceived complexity and high entry level requires an extreme degree of
- involvement for simple conversions, and excludes all but a few programmers.
-
- The lexical_cast function template offers a convenient and
- consistent form for supporting common conversions to and from arbitrary types
- when they are represented as text. The simplification it offers is in
- expression-level convenience for such conversions. For more involved
- conversions, such as where precision or formatting need tighter control than is
- offered by the default behavior of lexical_cast, the conventional
- stringstream approach is recommended. Where the conversions are
- numeric to numeric, numeric_cast
- may offer more reasonable behavior than lexical_cast.
-
- For a good discussion of the options and issues involved in string-based
- formatting, including comparison of stringstream, lexical_cast,
- and others, see Herb Sutter's article,
- The String Formatters of Manor Farm.
-
Returns the result of streaming arg into a
- standard library string-based stream and then out as a Target object.
- Where Target is either std::string
- or std::wstring, stream extraction takes the whole content
- of the string, including spaces, rather than relying on the default
- operator>> behavior.
- If the conversion is unsuccessful, a
- bad_lexical_cast exception is thrown.
-
- The requirements on the argument and result types are:
-
-
- Source is OutputStreamable, meaning that an operator<<
- is defined that takes a std::ostream or std::wostream object on the
- left hand side and an instance of the argument type on the right.
-
-
- Target is InputStreamable, meaning that an operator>>
- is defined that takes a std::istream or std::wistream object on the left hand side
- and an instance of the result type on the right.
-
-
- Both Source and Target are CopyConstructible [20.1.3].
-
-
- Target is DefaultConstructible, meaning that it is possible
- to default-initialize an object of that type [8.5, 20.1.4].
-
-
- The character type of the underlying stream is assumed to be char unless
- either the Source or the Target requires wide-character
- streaming, in which case the underlying stream uses wchar_t.
- Source types that require wide-character streaming are wchar_t,
- wchar_t *, and std::wstring. Target types that
- require wide-character streaming are wchar_t and std::wstring.
-
- Where a higher degree of control is required over conversions, std::stringstream
- and std::wstringstream offer a more appropriate path. Where non-stream-based conversions are
- required, lexical_cast
- is the wrong tool for the job and is not special-cased for such scenarios.
-
Call-by-const reference for the parameters. This requires partial specialization
- of class templates, so it doesn't work for MSVC 6, and it uses the original
- pass by value there.
-
-
The MSVC 6 support is deprecated, and will be removed in a future Boost
- version.
-
-
Earlier:
-
-
-
The previous version of lexical_cast used the default stream
- precision for reading and writing floating-point numbers. For numerics that
- have a corresponding specialization of std::numeric_limits, the
- current version now chooses a precision to match.
-
The previous version of lexical_cast did not support conversion
- to or from any wide-character-based types. For compilers with full language
- and library support for wide characters, lexical_cast now supports
- conversions from wchar_t, wchar_t *, and std::wstring
- and to wchar_t and std::wstring.
-
The previous version of lexical_cast assumed that the conventional
- stream extractor operators were sufficient for reading values. However, string
- I/O is asymmetric, with the result that spaces play the role of I/O separators
- rather than string content. The current version fixes this error for std::string
- and, where supported, std::wstring: lexical_cast<std::string>("Hello,
- World") succeeds instead of failing with a bad_lexical_cast
- exception.
-
The previous version of lexical_cast allowed unsafe and meaningless
- conversions to pointers. The current version now throws a bad_lexical_cast
- for conversions to pointers: lexical_cast<char *>("Goodbye, World")
- now throws an exception instead of causing undefined behavior.
-