#define BOOST_POLYMORPHIC_CAST_HPP
# include <boost/config.hpp>
-# include <boost/assert.hpp>
-# include <boost/throw_exception.hpp>
-# include <typeinfo>
#ifdef BOOST_HAS_PRAGMA_ONCE
# pragma once
#endif
+# include <boost/assert.hpp>
+# include <boost/core/addressof.hpp>
+# include <boost/core/enable_if.hpp>
+# include <boost/throw_exception.hpp>
+# include <boost/type_traits/is_reference.hpp>
+# include <boost/type_traits/remove_reference.hpp>
+
+# include <typeinfo>
+
namespace boost
{
// See the documentation for descriptions of how to choose between
// polymorphic_downcast ----------------------------------------------------//
- // BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited.
+ // BOOST_ASSERT() checked raw pointer polymorphic downcast. Crosscasts prohibited.
// WARNING: Because this cast uses BOOST_ASSERT(), it violates
// the One Definition Rule if used in multiple translation units
return static_cast<Target>(x);
}
+ // BOOST_ASSERT() checked reference 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 Julien Delacroix
+
+ template <class Target, class Source>
+ inline typename boost::enable_if_c<
+ boost::is_reference<Target>::value, Target
+ >::type polymorphic_downcast(Source& x)
+ {
+ typedef typename boost::remove_reference<Target>::type* target_pointer_type;
+ return *boost::polymorphic_downcast<target_pointer_type>(
+ boost::addressof(x)
+ );
+ }
+
} // namespace boost
#endif // BOOST_POLYMORPHIC_CAST_HPP