1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <memory>
10 
11 // weak_ptr
12 
13 // weak_ptr& operator=(const weak_ptr& r);
14 
15 #include <memory>
16 #include <type_traits>
17 #include <cassert>
18 #include <utility>
19 
20 #include "test_macros.h"
21 
22 struct B
23 {
24     static int count;
25 
BB26     B() {++count;}
BB27     B(const B&) {++count;}
~BB28     virtual ~B() {--count;}
29 };
30 
31 int B::count = 0;
32 
33 struct A
34     : public B
35 {
36     static int count;
37 
AA38     A() {++count;}
AA39     A(const A& other) : B(other) {++count;}
~AA40     ~A() {--count;}
41 };
42 
43 int A::count = 0;
44 
main(int,char **)45 int main(int, char**)
46 {
47     {
48         const std::shared_ptr<A> ps(new A);
49         const std::weak_ptr<A> pA(ps);
50         {
51             std::weak_ptr<A> pB;
52             pB = pA;
53             assert(B::count == 1);
54             assert(A::count == 1);
55             assert(pB.use_count() == 1);
56             assert(pA.use_count() == 1);
57         }
58         assert(pA.use_count() == 1);
59         assert(B::count == 1);
60         assert(A::count == 1);
61     }
62     assert(B::count == 0);
63     assert(A::count == 0);
64 
65     {
66         const std::shared_ptr<A> ps(new A);
67         std::weak_ptr<A> pA(ps);
68         {
69             std::weak_ptr<A> pB;
70             pB = std::move(pA);
71             assert(B::count == 1);
72             assert(A::count == 1);
73             assert(pB.use_count() == 1);
74         }
75         assert(B::count == 1);
76         assert(A::count == 1);
77     }
78     assert(B::count == 0);
79     assert(A::count == 0);
80 
81   return 0;
82 }
83