Add negative pointer cast tests.

This commit is contained in:
Peter Dimov
2016-09-10 18:43:22 +03:00
parent 190c06e25d
commit a14515a364
11 changed files with 244 additions and 15 deletions

View File

@ -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

View File

@ -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 ]
;
}

View 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 ) );
}

View 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 ) );
}

View 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 ) );
}

View 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 ) );
}

View 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 ) );
}

View 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 ) );
}

View 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 ) );
}

View 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 ) );
}

View 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 ) );
}