mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-29 20:37:13 +02:00
Relax dynamic_cast check; rephrase const_cast test to be more MSVC-friendly.
This commit is contained in:
@ -46,27 +46,12 @@ inline T* reinterpret_pointer_cast(U *ptr)
|
||||
|
||||
#if !defined( BOOST_NO_CXX11_SMART_PTR )
|
||||
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
#include <boost/type_traits/is_base_of.hpp>
|
||||
#include <boost/type_traits/is_pod.hpp>
|
||||
#include <boost/type_traits/has_virtual_destructor.hpp>
|
||||
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class T, class U>
|
||||
void assert_safe_moving_upcast() {
|
||||
BOOST_STATIC_ASSERT_MSG( !(boost::is_base_of<T, U>::value && !boost::is_pod<U>::value && !boost::has_virtual_destructor<T>::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<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_
|
||||
{
|
||||
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
detail::assert_safe_moving_upcast<T, U>();
|
||||
BOOST_STATIC_ASSERT_MSG( boost::has_virtual_destructor<T>::value, "The target of dynamic_pointer_cast must have a virtual destructor." );
|
||||
|
||||
T * p = dynamic_cast<T*>( r.get() );
|
||||
if( p ) r.release();
|
||||
|
@ -94,16 +94,6 @@ static void test_static_cast()
|
||||
|
||||
static void test_const_cast()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int const> p1( new int );
|
||||
int const * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int> p2 = boost::const_pointer_cast<int>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int> 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<int> p3 = boost::const_pointer_cast<int>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p3.get(), q1 );
|
||||
}
|
||||
|
||||
#if !defined( BOOST_MSVC ) || BOOST_MSVC >= 1900
|
||||
|
||||
{
|
||||
std::unique_ptr<int const[]> p1( new int[ 1 ] );
|
||||
int const * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int[]> p2 = boost::const_pointer_cast<int[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
{
|
||||
std::unique_ptr<int[]> 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<int[]> p3 = boost::const_pointer_cast<int[]>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p3.get(), q1 );
|
||||
}
|
||||
}
|
||||
|
||||
struct B2
|
||||
|
Reference in New Issue
Block a user