diff --git a/cast.htm b/cast.htm deleted file mode 100644 index c8d14c7..0000000 --- a/cast.htm +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - - - - - Header boost/cast.hpp Documentation - - - - -

boost.png (6897 bytes)Header boost/cast.hpp

- -

Cast Functions

- -

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 - verify these function templates work as expected.

- -

Polymorphic casts

- -

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

- -
-
namespace boost {
-
-template <class Derived, class Base>
-inline Derived polymorphic_cast(Base* x);
-// Throws: std::bad_cast if ( dynamic_cast<Derived>(x) == 0 )
-// Returns: dynamic_cast<Derived>(x)
-
-template <class Derived, class Base>
-inline Derived polymorphic_downcast(Base* x);
-// Effects: assert( dynamic_cast<Derived>(x) == x );
-// Returns: static_cast<Derived>(x)
-
-}
-
-
- -

polymorphic_downcast example

- -
-
#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

-
- -

Revised - June 23, 2005

- - - - diff --git a/include/boost/cast.hpp b/include/boost/cast.hpp deleted file mode 100644 index 0018924..0000000 --- a/include/boost/cast.hpp +++ /dev/null @@ -1,94 +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 - -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) - { - 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_ASSERT( dynamic_cast(x) == x ); // detect logic error - return static_cast(x); - } - -} // namespace boost - -# include - -#endif // BOOST_CAST_HPP diff --git a/include/boost/implicit_cast.hpp b/include/boost/implicit_cast.hpp deleted file mode 100755 index 5b1cd92..0000000 --- a/include/boost/implicit_cast.hpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright David Abrahams 2003. -// 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) -#ifndef IMPLICIT_CAST_DWA200356_HPP -# define IMPLICIT_CAST_DWA200356_HPP - -# include - -namespace boost { - -// implementation originally suggested by C. Green in -// http://lists.boost.org/MailArchives/boost/msg00886.php - -// The use of identity creates a non-deduced form, so that the -// explicit template argument must be supplied -template -inline T implicit_cast (typename mpl::identity::type x) { - return x; -} - -// incomplete return type now is here -//template -//void implicit_cast (...); - -} // namespace boost - - -#endif // IMPLICIT_CAST_DWA200356_HPP diff --git a/index.html b/index.html index fbfe63a..73b6ba5 100644 --- a/index.html +++ b/index.html @@ -1,49 +1,16 @@ + + + - - - - - -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 -

- + +Automatic redirection failed, please go to +../../doc/html/boost_lexical_cast.html - diff --git a/lexical_cast.htm b/lexical_cast.htm deleted file mode 100644 index 73b6ba5..0000000 --- a/lexical_cast.htm +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - -Automatic redirection failed, please go to -../../doc/html/boost_lexical_cast.html - - diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 3acf6b4..0022600 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -1,5 +1,5 @@ # Copyright (C) 2001-2003 Douglas Gregor -# Copyright (C) 2011-2013 Antony Polukhin +# Copyright (C) 2011-2014 Antony Polukhin # # 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) @@ -28,11 +28,7 @@ feature.feature nowchar : on : feature.compose on : /Zc:wchar_t- ; test-suite conversion - : [ run implicit_cast.cpp ] - [ compile-fail implicit_cast_fail.cpp ] - [ run cast_test.cpp ] - [ run numeric_cast_test.cpp ] - [ run lexical_cast_test.cpp ] + : [ run lexical_cast_test.cpp ] [ run lexical_cast_loopback_test.cpp ] [ run lexical_cast_abstract_test.cpp ] [ run lexical_cast_noncopyable_test.cpp ] diff --git a/test/cast_test.cpp b/test/cast_test.cpp deleted file mode 100644 index ad6b18f..0000000 --- a/test/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/test/implicit_cast.cpp b/test/implicit_cast.cpp deleted file mode 100644 index 0cad067..0000000 --- a/test/implicit_cast.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright David Abrahams 2003. -// 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) - -#include -#include -#include -using boost::implicit_cast; -using boost::type; - -template -type check_return(T) { return type(); } - -struct foo -{ - foo(char const*) {} - operator long() const { return 0; } -}; - -typedef type long_type; -typedef type foo_type; - -int main() -{ - type x = check_return(boost::implicit_cast(1)); - BOOST_TEST(boost::implicit_cast(1) == 1L); - - type f = check_return(boost::implicit_cast("hello")); - type z = check_return(boost::implicit_cast(foo("hello"))); - - // warning supression: - (void)x; - (void)f; - (void)z; - - return boost::report_errors(); -} diff --git a/test/implicit_cast_fail.cpp b/test/implicit_cast_fail.cpp deleted file mode 100644 index 442fa4c..0000000 --- a/test/implicit_cast_fail.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright David Abrahams 2003. -// 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) - -#include -#include - -#define BOOST_INCLUDE_MAIN -#include - -using boost::implicit_cast; - -struct foo -{ - explicit foo(char const*) {} -}; - -int test_main(int, char*[]) -{ - foo x = implicit_cast("foobar"); - (void)x; // warning suppression. - BOOST_CHECK(false); // suppressing warning about 'boost::unit_test::{anonymous}::unit_test_log' defined but not used - return 0; -} - diff --git a/test/numeric_cast_test.cpp b/test/numeric_cast_test.cpp deleted file mode 100644 index 015776d..0000000 --- a/test/numeric_cast_test.cpp +++ /dev/null @@ -1,101 +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 - -#include "boost/test/minimal.hpp" - -# if SCHAR_MAX == LONG_MAX -# error "This test program doesn't work if SCHAR_MAX == LONG_MAX" -# endif - -using namespace boost; -using std::cout; - - -int test_main( int , char * [] ) -{ - -# ifdef NDEBUG - cout << "NDEBUG is defined\n"; -# else - cout << "NDEBUG is not defined\n"; -# endif - - cout << "\nBeginning tests...\n"; - -// test implicit_cast and numeric_cast -------------------------------------// - - // tests which should succeed - long small_value = 1; - long small_negative_value = -1; - long large_value = LONG_MAX; - long large_negative_value = LONG_MIN; - signed char c = 0; - - c = static_cast(large_value); - - c = numeric_cast( small_value ); - BOOST_CHECK( c == 1 ); - c = 0; - c = numeric_cast( small_value ); - BOOST_CHECK( c == 1 ); - c = 0; - c = numeric_cast( small_negative_value ); - BOOST_CHECK( c == -1 ); - - // These tests courtesy of Joe R NWP Swatosh - BOOST_CHECK( 0.0f == numeric_cast( 0.0 ) ); - BOOST_CHECK( 0.0 == numeric_cast( 0.0 ) ); - - // tests which should result in errors being detected - - bool caught_exception = false; - try { c = numeric_cast( large_value ); } - catch (bad_numeric_cast) - { cout<<"caught bad_numeric_cast #1\n"; caught_exception = true; } - BOOST_CHECK ( caught_exception ); - - caught_exception = false; - try { c = numeric_cast( large_negative_value ); } - catch (bad_numeric_cast) - { cout<<"caught bad_numeric_cast #2\n"; caught_exception = true; } - BOOST_CHECK ( caught_exception ); - - unsigned long ul; - caught_exception = false; - try { ul = numeric_cast( large_negative_value ); } - catch (bad_numeric_cast) - { cout<<"caught bad_numeric_cast #3\n"; caught_exception = true; } - BOOST_CHECK ( caught_exception ); - - caught_exception = false; - try { ul = numeric_cast( small_negative_value ); } - catch (bad_numeric_cast) - { cout<<"caught bad_numeric_cast #4\n"; caught_exception = true; } - BOOST_CHECK ( caught_exception ); - - caught_exception = false; - try { numeric_cast( DBL_MAX ); } - catch (bad_numeric_cast) - { cout<<"caught bad_numeric_cast #5\n"; caught_exception = true; } - BOOST_CHECK ( caught_exception ); - - (void)ul; // Supressing GCC warning about set but unused wariable - return 0 ; -}