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
15#include <boost/smart_ptr/enable_shared_from_raw.hpp>
16#include <boost/weak_ptr.hpp>
17#include <boost/core/lightweight_test.hpp>
18
19class X: public boost::enable_shared_from_raw
20{
21public:
22
23 X()
24 {
25 boost::weak_ptr<X> p1 = boost::weak_from_raw( p: this );
26 BOOST_TEST( !p1.expired() );
27
28 boost::weak_ptr<X> p2 = boost::weak_from_raw( p: this );
29 BOOST_TEST( !p2.expired() );
30 BOOST_TEST( !( p1 < p2 ) && !( p2 < p1 ) );
31
32 boost::weak_ptr<X> p3 = boost::shared_from_raw( p: this );
33 BOOST_TEST( !( p1 < p3 ) && !( p3 < p1 ) );
34
35 boost::weak_ptr<X> p4 = boost::weak_from_raw( p: this );
36 BOOST_TEST( !p4.expired() );
37 BOOST_TEST( !( p3 < p4 ) && !( p4 < p3 ) );
38 BOOST_TEST( !( p1 < p4 ) && !( p4 < p1 ) );
39 }
40};
41
42int main()
43{
44 boost::shared_ptr< X > px( new X );
45 return boost::report_errors();
46}
47

source code of boost/libs/smart_ptr/test/weak_from_raw_test3.cpp