mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-31 21:24:40 +02:00
Add negative pointer cast tests.
This commit is contained in:
@@ -90,6 +90,8 @@ template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std
|
|||||||
//static_pointer_cast overload for std::unique_ptr
|
//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
|
template<class T, class U> std::unique_ptr<T> static_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
|
(void) static_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
typedef typename std::unique_ptr<T>::element_type E;
|
typedef typename std::unique_ptr<T>::element_type E;
|
||||||
|
|
||||||
detail::assert_safe_moving_upcast<T, U>();
|
detail::assert_safe_moving_upcast<T, U>();
|
||||||
@@ -100,6 +102,8 @@ template<class T, class U> std::unique_ptr<T> static_pointer_cast( std::unique_p
|
|||||||
//dynamic_pointer_cast overload for std::unique_ptr
|
//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
|
template<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
|
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
detail::assert_safe_moving_upcast<T, U>();
|
detail::assert_safe_moving_upcast<T, U>();
|
||||||
|
|
||||||
T * p = dynamic_cast<T*>( r.get() );
|
T * p = dynamic_cast<T*>( r.get() );
|
||||||
@@ -110,6 +114,8 @@ template<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_
|
|||||||
//const_pointer_cast overload for std::unique_ptr
|
//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
|
template<class T, class U> std::unique_ptr<T> const_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
|
(void) const_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
typedef typename std::unique_ptr<T>::element_type E;
|
typedef typename std::unique_ptr<T>::element_type E;
|
||||||
|
|
||||||
return std::unique_ptr<T>( const_cast<E*>( r.release() ) );
|
return std::unique_ptr<T>( const_cast<E*>( r.release() ) );
|
||||||
@@ -118,6 +124,8 @@ template<class T, class U> std::unique_ptr<T> const_pointer_cast( std::unique_pt
|
|||||||
//reinterpret_pointer_cast overload for std::unique_ptr
|
//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
|
template<class T, class U> std::unique_ptr<T> reinterpret_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
|
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
|
||||||
|
|
||||||
typedef typename std::unique_ptr<T>::element_type E;
|
typedef typename std::unique_ptr<T>::element_type E;
|
||||||
|
|
||||||
return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) );
|
return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) );
|
||||||
|
@@ -189,5 +189,17 @@ import testing ;
|
|||||||
|
|
||||||
[ run pointer_cast_test2.cpp ]
|
[ run pointer_cast_test2.cpp ]
|
||||||
[ run pointer_cast_test3.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