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 
19 #include "test_macros.h"
20 
21 struct B
22 {
23     static int count;
24 
BB25     B() {++count;}
BB26     B(const B&) {++count;}
~BB27     virtual ~B() {--count;}
28 };
29 
30 int B::count = 0;
31 
32 struct A
33     : public B
34 {
35     static int count;
36 
AA37     A() {++count;}
AA38     A(const A& other) : B(other) {++count;}
~AA39     ~A() {--count;}
40 };
41 
42 int A::count = 0;
43 
main(int,char **)44 int main(int, char**)
45 {
46     {
47         const std::shared_ptr<A> ps(new A);
48         const std::weak_ptr<A> pA(ps);
49         {
50             std::weak_ptr<A> pB;
51             pB = pA;
52             assert(B::count == 1);
53             assert(A::count == 1);
54             assert(pB.use_count() == 1);
55             assert(pA.use_count() == 1);
56         }
57         assert(pA.use_count() == 1);
58         assert(B::count == 1);
59         assert(A::count == 1);
60     }
61     assert(B::count == 0);
62     assert(A::count == 0);
63 
64     {
65         const std::shared_ptr<A> ps(new A);
66         std::weak_ptr<A> pA(ps);
67         {
68             std::weak_ptr<A> pB;
69             pB = std::move(pA);
70             assert(B::count == 1);
71             assert(A::count == 1);
72             assert(pB.use_count() == 1);
73         }
74         assert(B::count == 1);
75         assert(A::count == 1);
76     }
77     assert(B::count == 0);
78     assert(A::count == 0);
79 
80   return 0;
81 }
82