From 23e5f7cca38eb3487b0a527abe017c09456f4782 Mon Sep 17 00:00:00 2001
From: Darin Adler The The header boost/cast.hpp
provides polymorphic_cast, polymorphic_downcast,
and numeric_cast function templates designed
to complement the C++ built-in casts. The program cast_test.cpp can be used to
+
+ The program cast_test.cpp can be used to
verify these function templates work as expected. 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
+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
+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. Header
+
+
Header
boost/cast.hpp
Cast Functions
-header boost/cast.hpp
+
+Polymorphic casts
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 polymorphic_cast template performs a dynamic_cast, +test. The polymorphic_cast template performs a dynamic_cast, and throws an exception if the dynamic_cast returns 0.
A polymorphic_downcast is preferred when debug-mode tests will cover 100% of the object types possibly cast and when non-debug-mode efficiency is an issue. If these two conditions are not present, polymorphic_cast is -preferred. It must also be used for crosscasts. It does an assert( +preferred. It must also be used for crosscasts. It does an assert( dynamic_cast<Derived>(x) == x ) where x is the base pointer, ensuring that not only is a non-zero pointer returned, but also that it correct in the presence of multiple inheritance. 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]
+defined across translation units. [See ISO Std 3.2]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 +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.
Revised 06 January, 2001
© Copyright boost.org 1999. Permission to copy, use, modify, sell and