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 | // template<class Y> weak_ptr& operator=(const shared_ptr<Y>& 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 | |
25 | B() {++count;} |
26 | B(const B&) {++count;} |
27 | virtual ~B() {--count;} |
28 | }; |
29 | |
30 | int B::count = 0; |
31 | |
32 | struct A |
33 | : public B |
34 | { |
35 | static int count; |
36 | |
37 | A() {++count;} |
38 | A(const A& other) : B(other) {++count;} |
39 | ~A() {--count;} |
40 | }; |
41 | |
42 | int A::count = 0; |
43 | |
44 | int main(int, char**) |
45 | { |
46 | { |
47 | const std::shared_ptr<A> pA(new A); |
48 | assert(pA.use_count() == 1); |
49 | { |
50 | std::weak_ptr<B> 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 | #if TEST_STD_VER > 14 |
65 | { |
66 | const std::shared_ptr<A[]> p1(new A[8]); |
67 | assert(p1.use_count() == 1); |
68 | { |
69 | std::weak_ptr<const A[]> p2; |
70 | p2 = p1; |
71 | assert(A::count == 8); |
72 | assert(p2.use_count() == 1); |
73 | assert(p1.use_count() == 1); |
74 | } |
75 | assert(p1.use_count() == 1); |
76 | assert(A::count == 8); |
77 | } |
78 | assert(A::count == 0); |
79 | #endif |
80 | |
81 | return 0; |
82 | } |
83 | |