Ticket #9799: cast.2.diff
File cast.2.diff, 1.9 KB (added by , 9 years ago) |
---|
-
cast.hpp
old new 49 49 # include <boost/type.hpp> 50 50 # include <boost/limits.hpp> 51 51 # include <boost/detail/select_type.hpp> 52 # include <boost/mpl/if.hpp> 53 # include <boost/type_traits/remove_pointer.hpp> 54 # include <boost/type_traits/is_virtual_base_of.hpp> 52 55 53 56 // It has been demonstrated numerous times that MSVC 6.0 fails silently at link 54 57 // time if you use a template function which has template parameters that don't … … 82 85 83 86 // polymorphic_downcast ----------------------------------------------------// 84 87 88 namespace detail 89 { 90 91 template<typename Out, typename In> 92 struct polymorphic_static_downcast 93 { 94 static Out eval(In* in) 95 { return static_cast<Out>( in ); } 96 }; 97 98 template<typename Out, typename In> 99 struct polymorphic_dynamic_downcast 100 { 101 static Out eval(In* in) 102 { return dynamic_cast<Out>( in ); } 103 }; 104 105 } 106 85 107 // BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited. 86 108 87 109 // WARNING: Because this cast uses BOOST_ASSERT(), it violates … … 95 117 inline Target polymorphic_downcast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET) 96 118 { 97 119 BOOST_ASSERT( dynamic_cast<Target>(x) == x ); // detect logic error 98 return static_cast<Target>(x); 120 121 typedef typename boost::remove_pointer<Target>::type out_class; 122 typedef Source in_class; 123 124 typedef boost::is_virtual_base_of<in_class, out_class> dyn; 125 typedef typename boost::mpl::if_<dyn 126 , detail::polymorphic_dynamic_downcast<Target, Source> 127 , detail::polymorphic_static_downcast<Target, Source> 128 >::type impl; 129 130 return impl::eval(x); 99 131 } 100 132 101 133 # undef BOOST_EXPLICIT_DEFAULT_TARGET