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