mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-30 04:47:12 +02:00
Add negative pointer cast tests.
This commit is contained in:
@ -79,48 +79,56 @@ using std::const_pointer_cast;
|
||||
//reinterpret_pointer_cast overload for std::shared_ptr
|
||||
template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std::shared_ptr<U> & r ) BOOST_NOEXCEPT
|
||||
{
|
||||
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
|
||||
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
typedef typename std::shared_ptr<T>::element_type E;
|
||||
typedef typename std::shared_ptr<T>::element_type E;
|
||||
|
||||
E * p = reinterpret_cast< E* >( r.get() );
|
||||
return std::shared_ptr<T>( r, p );
|
||||
E * p = reinterpret_cast< E* >( r.get() );
|
||||
return std::shared_ptr<T>( r, p );
|
||||
}
|
||||
|
||||
//static_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> static_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
(void) static_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
detail::assert_safe_moving_upcast<T, U>();
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
|
||||
return std::unique_ptr<T>( static_cast<E*>( r.release() ) );
|
||||
detail::assert_safe_moving_upcast<T, U>();
|
||||
|
||||
return std::unique_ptr<T>( static_cast<E*>( r.release() ) );
|
||||
}
|
||||
|
||||
//dynamic_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
detail::assert_safe_moving_upcast<T, U>();
|
||||
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
T * p = dynamic_cast<T*>( r.get() );
|
||||
if( p ) r.release();
|
||||
return std::unique_ptr<T>( p );
|
||||
detail::assert_safe_moving_upcast<T, U>();
|
||||
|
||||
T * p = dynamic_cast<T*>( r.get() );
|
||||
if( p ) r.release();
|
||||
return std::unique_ptr<T>( p );
|
||||
}
|
||||
|
||||
//const_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> const_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
(void) const_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
return std::unique_ptr<T>( const_cast<E*>( r.release() ) );
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
|
||||
return std::unique_ptr<T>( const_cast<E*>( r.release() ) );
|
||||
}
|
||||
|
||||
//reinterpret_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> reinterpret_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) );
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
|
||||
return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) );
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
@ -189,5 +189,17 @@ import testing ;
|
||||
|
||||
[ run pointer_cast_test2.cpp ]
|
||||
[ run pointer_cast_test3.cpp ]
|
||||
|
||||
[ compile-fail pointer_cast_st_fail.cpp ]
|
||||
[ compile-fail pointer_cast_st_fail2.cpp ]
|
||||
[ compile-fail pointer_cast_st_fail3.cpp ]
|
||||
|
||||
[ compile-fail pointer_cast_co_fail.cpp ]
|
||||
[ compile-fail pointer_cast_co_fail2.cpp ]
|
||||
[ compile-fail pointer_cast_co_fail3.cpp ]
|
||||
|
||||
[ compile-fail pointer_cast_dy_fail.cpp ]
|
||||
[ compile-fail pointer_cast_dy_fail2.cpp ]
|
||||
[ compile-fail pointer_cast_dy_fail3.cpp ]
|
||||
;
|
||||
}
|
||||
|
18
test/pointer_cast_co_fail.cpp
Normal file
18
test/pointer_cast_co_fail.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
//
|
||||
// A negative test for unique_ptr const_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
std::unique_ptr<int[]> p2 = boost::const_pointer_cast<int[]>( std::move( p1 ) );
|
||||
}
|
18
test/pointer_cast_co_fail2.cpp
Normal file
18
test/pointer_cast_co_fail2.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
//
|
||||
// A negative test for unique_ptr const_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
std::unique_ptr<int> p2 = boost::const_pointer_cast<int>( std::move( p1 ) );
|
||||
}
|
29
test/pointer_cast_co_fail3.cpp
Normal file
29
test/pointer_cast_co_fail3.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
//
|
||||
// A negative test for unique_ptr const_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct D: B
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<D[]> p1( new D[ 1 ] );
|
||||
std::unique_ptr<B[]> p2 = boost::const_pointer_cast<B[]>( std::move( p1 ) );
|
||||
}
|
25
test/pointer_cast_dy_fail.cpp
Normal file
25
test/pointer_cast_dy_fail.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
//
|
||||
// A negative test for unique_ptr dynamic_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<B> p1( new B );
|
||||
std::unique_ptr<B[]> p2 = boost::dynamic_pointer_cast<B[]>( std::move( p1 ) );
|
||||
}
|
25
test/pointer_cast_dy_fail2.cpp
Normal file
25
test/pointer_cast_dy_fail2.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
//
|
||||
// A negative test for unique_ptr dynamic_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<B[]> p1( new B[ 1 ] );
|
||||
std::unique_ptr<B> p2 = boost::dynamic_pointer_cast<B>( std::move( p1 ) );
|
||||
}
|
29
test/pointer_cast_dy_fail3.cpp
Normal file
29
test/pointer_cast_dy_fail3.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
//
|
||||
// A negative test for unique_ptr dynamic_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct D: B
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<D[]> p1( new D[ 1 ] );
|
||||
std::unique_ptr<B[]> p2 = boost::dynamic_pointer_cast<B[]>( std::move( p1 ) );
|
||||
}
|
18
test/pointer_cast_st_fail.cpp
Normal file
18
test/pointer_cast_st_fail.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
//
|
||||
// A negative test for unique_ptr static_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
std::unique_ptr<int[]> p2 = boost::static_pointer_cast<int[]>( std::move( p1 ) );
|
||||
}
|
18
test/pointer_cast_st_fail2.cpp
Normal file
18
test/pointer_cast_st_fail2.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
//
|
||||
// A negative test for unique_ptr static_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
std::unique_ptr<int> p2 = boost::static_pointer_cast<int>( std::move( p1 ) );
|
||||
}
|
29
test/pointer_cast_st_fail3.cpp
Normal file
29
test/pointer_cast_st_fail3.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
//
|
||||
// A negative test for unique_ptr static_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct D: B
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<D[]> p1( new D[ 1 ] );
|
||||
std::unique_ptr<B[]> p2 = boost::static_pointer_cast<B[]>( std::move( p1 ) );
|
||||
}
|
Reference in New Issue
Block a user