esft_second_ptr_test.cpp 1010 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // esft_second_ptr_test.cpp
  3. //
  4. // This test has been extracted from a real
  5. // scenario that occurs in Boost.Python
  6. //
  7. // Copyright 2009 Peter Dimov
  8. //
  9. // Distributed under the Boost Software License, Version 1.0.
  10. // See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt
  12. //
  13. #include <boost/enable_shared_from_this.hpp>
  14. #include <boost/shared_ptr.hpp>
  15. #include <boost/detail/lightweight_test.hpp>
  16. //
  17. class X: public boost::enable_shared_from_this<X>
  18. {
  19. };
  20. void null_deleter( void const* )
  21. {
  22. }
  23. int main()
  24. {
  25. boost::shared_ptr<X> px( new X );
  26. {
  27. boost::shared_ptr<X> px2( px.get(), null_deleter );
  28. BOOST_TEST( px == px2 );
  29. }
  30. try
  31. {
  32. boost::shared_ptr< X > qx = px->shared_from_this();
  33. BOOST_TEST( px == qx );
  34. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  35. }
  36. catch( boost::bad_weak_ptr const& )
  37. {
  38. BOOST_ERROR( "px->shared_from_this() failed" );
  39. }
  40. return boost::report_errors();
  41. }