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 // shared_ptr<T> lock() const;
14 
15 #include <memory>
16 #include <cassert>
17 
18 struct A
19 {
20     static int count;
21 
22     A() {++count;}
23     A(const A&) {++count;}
24     ~A() {--count;}
25 };
26 
27 int A::count = 0;
28 
29 int main()
30 {
31     {
32         std::weak_ptr<A> wp;
33         std::shared_ptr<A> sp = wp.lock();
34         assert(sp.use_count() == 0);
35         assert(sp.get() == 0);
36         assert(A::count == 0);
37     }
38     {
39         std::shared_ptr<A> sp0(new A);
40         std::weak_ptr<A> wp(sp0);
41         std::shared_ptr<A> sp = wp.lock();
42         assert(sp.use_count() == 2);
43         assert(sp.get() == sp0.get());
44         assert(A::count == 1);
45     }
46     assert(A::count == 0);
47     {
48         std::shared_ptr<A> sp0(new A);
49         std::weak_ptr<A> wp(sp0);
50         sp0.reset();
51         std::shared_ptr<A> sp = wp.lock();
52         assert(sp.use_count() == 0);
53         assert(sp.get() == 0);
54         assert(A::count == 0);
55     }
56     assert(A::count == 0);
57 }
58