1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <memory>
11 
12 // weak_ptr
13 
14 // template<class Y> weak_ptr& operator=(const shared_ptr<Y>& r);
15 
16 #include <memory>
17 #include <type_traits>
18 #include <cassert>
19 
20 struct B
21 {
22     static int count;
23 
BB24     B() {++count;}
BB25     B(const B&) {++count;}
~BB26     virtual ~B() {--count;}
27 };
28 
29 int B::count = 0;
30 
31 struct A
32     : public B
33 {
34     static int count;
35 
AA36     A() {++count;}
AA37     A(const A&) {++count;}
~AA38     ~A() {--count;}
39 };
40 
41 int A::count = 0;
42 
main()43 int main()
44 {
45     {
46         const std::shared_ptr<A> pA(new A);
47         {
48             std::weak_ptr<B> pB;
49             pB = pA;
50             assert(B::count == 1);
51             assert(A::count == 1);
52             assert(pB.use_count() == 1);
53             assert(pA.use_count() == 1);
54         }
55         assert(pA.use_count() == 1);
56         assert(B::count == 1);
57         assert(A::count == 1);
58     }
59     assert(B::count == 0);
60     assert(A::count == 0);
61 }
62