weak_from_raw_test5.cpp 928 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // weak_from_raw_test5.cpp
  3. //
  4. // Tests whether pointers returned from weak_from_raw
  5. // expire properly
  6. //
  7. // Copyright 2015 Peter Dimov
  8. //
  9. // Distributed under the Boost Software License, Version 1.0.
  10. //
  11. // See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt
  13. //
  14. #include <boost/smart_ptr/enable_shared_from_raw.hpp>
  15. #include <boost/weak_ptr.hpp>
  16. #include <boost/core/lightweight_test.hpp>
  17. class X: public boost::enable_shared_from_raw
  18. {
  19. public:
  20. explicit X( boost::weak_ptr< X > & r )
  21. {
  22. r = boost::weak_from_raw( this );
  23. }
  24. };
  25. int main()
  26. {
  27. boost::weak_ptr<X> p1, p2;
  28. {
  29. boost::shared_ptr< X > px( new X( p1 ) );
  30. p2 = boost::weak_from_raw( px.get() );
  31. BOOST_TEST( !p1.expired() );
  32. BOOST_TEST( !p2.expired() );
  33. }
  34. BOOST_TEST( p1.expired() );
  35. BOOST_TEST( p2.expired() );
  36. return boost::report_errors();
  37. }