1 #include <boost/config.hpp> 2 3 // 4 // weak_from_this_test.cpp 5 // 6 // Copyright (c) 2002, 2003, 2015 Peter Dimov 7 // 8 // Distributed under the Boost Software License, Version 1.0. 9 // 10 // See accompanying file LICENSE_1_0.txt or copy at 11 // http://www.boost.org/LICENSE_1_0.txt 12 // 13 14 15 #include <boost/enable_shared_from_this.hpp> 16 #include <boost/shared_ptr.hpp> 17 #include <boost/weak_ptr.hpp> 18 #include <boost/core/lightweight_test.hpp> 19 20 class V: public boost::enable_shared_from_this<V> 21 { 22 }; 23 test()24void test() 25 { 26 boost::shared_ptr<V> p( new V ); 27 28 boost::weak_ptr<V> q = p; 29 BOOST_TEST( !q.expired() ); 30 31 boost::weak_ptr<V> q2 = p->weak_from_this(); 32 BOOST_TEST( !q2.expired() ); 33 BOOST_TEST( !(q < q2) && !(q2 < q) ); 34 35 V v2( *p ); 36 37 boost::weak_ptr<V> q3 = v2.weak_from_this(); 38 BOOST_TEST( q3.expired() ); 39 40 *p = V(); 41 42 boost::weak_ptr<V> q4 = p->weak_from_this(); 43 BOOST_TEST( !q4.expired() ); 44 BOOST_TEST( !(q < q4) && !(q4 < q) ); 45 BOOST_TEST( !(q2 < q4) && !(q4 < q2) ); 46 } 47 main()48int main() 49 { 50 test(); 51 return boost::report_errors(); 52 } 53