From 5595622c3eb401dd4d8aa5fab8e888f7edf873d5 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 10 Sep 2016 20:18:37 +0300 Subject: [PATCH] Relax dynamic_cast check; rephrase const_cast test to be more MSVC-friendly. --- include/boost/pointer_cast.hpp | 19 ++---------------- test/pointer_cast_test2.cpp | 36 +++++++++++----------------------- 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/include/boost/pointer_cast.hpp b/include/boost/pointer_cast.hpp index 7e04512..5af4971 100644 --- a/include/boost/pointer_cast.hpp +++ b/include/boost/pointer_cast.hpp @@ -46,27 +46,12 @@ inline T* reinterpret_pointer_cast(U *ptr) #if !defined( BOOST_NO_CXX11_SMART_PTR ) -#include - -#include -#include #include - +#include #include namespace boost { -namespace detail { - -template -void assert_safe_moving_upcast() { - BOOST_STATIC_ASSERT_MSG( !(boost::is_base_of::value && !boost::is_pod::value && !boost::has_virtual_destructor::value) - , "Upcast from a non-POD child to a base without virtual destructor is unsafe, because the child's destructor " - "will not be called when the base pointer is deleted. Consider using shared_ptr for such types."); -} - -} - //static_pointer_cast overload for std::shared_ptr using std::static_pointer_cast; @@ -102,7 +87,7 @@ template std::unique_ptr dynamic_pointer_cast( std::unique_ { (void) dynamic_cast< T* >( static_cast< U* >( 0 ) ); - detail::assert_safe_moving_upcast(); + BOOST_STATIC_ASSERT_MSG( boost::has_virtual_destructor::value, "The target of dynamic_pointer_cast must have a virtual destructor." ); T * p = dynamic_cast( r.get() ); if( p ) r.release(); diff --git a/test/pointer_cast_test2.cpp b/test/pointer_cast_test2.cpp index b8575b4..eb28a5d 100644 --- a/test/pointer_cast_test2.cpp +++ b/test/pointer_cast_test2.cpp @@ -94,16 +94,6 @@ static void test_static_cast() static void test_const_cast() { - { - std::unique_ptr p1( new int ); - int const * q1 = p1.get(); - - std::unique_ptr p2 = boost::const_pointer_cast( std::move( p1 ) ); - - BOOST_TEST( p1.get() == 0 ); - BOOST_TEST_EQ( p2.get(), q1 ); - } - { std::unique_ptr p1( new int ); int * q1 = p1.get(); @@ -112,22 +102,13 @@ static void test_const_cast() BOOST_TEST( p1.get() == 0 ); BOOST_TEST_EQ( p2.get(), q1 ); + + std::unique_ptr p3 = boost::const_pointer_cast( std::move( p2 ) ); + + BOOST_TEST( p2.get() == 0 ); + BOOST_TEST_EQ( p3.get(), q1 ); } -#if !defined( BOOST_MSVC ) || BOOST_MSVC >= 1900 - - { - std::unique_ptr p1( new int[ 1 ] ); - int const * q1 = p1.get(); - - std::unique_ptr p2 = boost::const_pointer_cast( std::move( p1 ) ); - - BOOST_TEST( p1.get() == 0 ); - BOOST_TEST_EQ( p2.get(), q1 ); - } - -#endif - { std::unique_ptr p1( new int[ 1 ] ); int * q1 = p1.get(); @@ -136,7 +117,12 @@ static void test_const_cast() BOOST_TEST( p1.get() == 0 ); BOOST_TEST_EQ( p2.get(), q1 ); - } + + std::unique_ptr p3 = boost::const_pointer_cast( std::move( p2 ) ); + + BOOST_TEST( p2.get() == 0 ); + BOOST_TEST_EQ( p3.get(), q1 ); + } } struct B2