weak_from_raw_test3.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // weak_from_raw_test3.cpp
  3. //
  4. // Test that weak_from_raw and shared_from_raw
  5. // return consistent values from a constructor
  6. //
  7. // Copyright (c) 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. X()
  21. {
  22. boost::weak_ptr<X> p1 = boost::weak_from_raw( this );
  23. BOOST_TEST( !p1.expired() );
  24. boost::weak_ptr<X> p2 = boost::weak_from_raw( this );
  25. BOOST_TEST( !p2.expired() );
  26. BOOST_TEST( !( p1 < p2 ) && !( p2 < p1 ) );
  27. boost::weak_ptr<X> p3 = boost::shared_from_raw( this );
  28. BOOST_TEST( !( p1 < p3 ) && !( p3 < p1 ) );
  29. boost::weak_ptr<X> p4 = boost::weak_from_raw( this );
  30. BOOST_TEST( !p4.expired() );
  31. BOOST_TEST( !( p3 < p4 ) && !( p4 < p3 ) );
  32. BOOST_TEST( !( p1 < p4 ) && !( p4 < p1 ) );
  33. }
  34. };
  35. int main()
  36. {
  37. boost::shared_ptr< X > px( new X );
  38. return boost::report_errors();
  39. }