shared_from_raw_test4.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // shared_from_raw_test4 - based on esft_void_test.cpp
  3. //
  4. // Copyright 2009, 2014 Peter Dimov
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. //
  8. // See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt
  10. //
  11. #include <boost/smart_ptr/enable_shared_from_raw.hpp>
  12. #include <boost/shared_ptr.hpp>
  13. #include <boost/detail/lightweight_test.hpp>
  14. //
  15. class X: public boost::enable_shared_from_raw
  16. {
  17. };
  18. int main()
  19. {
  20. boost::shared_ptr< void const volatile > pv( new X );
  21. boost::shared_ptr< void > pv2 = boost::const_pointer_cast< void >( pv );
  22. boost::shared_ptr< X > px = boost::static_pointer_cast< X >( pv2 );
  23. try
  24. {
  25. boost::shared_ptr< X > qx = boost::shared_from_raw( px.get() );
  26. BOOST_TEST( px == qx );
  27. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  28. }
  29. catch( boost::bad_weak_ptr const& )
  30. {
  31. BOOST_ERROR( "shared_from_this( px.get() ) failed" );
  32. }
  33. boost::shared_ptr< X const volatile > px2( px );
  34. try
  35. {
  36. boost::shared_ptr< X const volatile > qx2 = boost::shared_from_raw( px2.get() );
  37. BOOST_TEST( px2 == qx2 );
  38. BOOST_TEST( !( px2 < qx2 ) && !( qx2 < px2 ) );
  39. }
  40. catch( boost::bad_weak_ptr const& )
  41. {
  42. BOOST_ERROR( "shared_from_this( px2.get() ) failed" );
  43. }
  44. return boost::report_errors();
  45. }