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 // shared_ptr
12 
13 // template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
14 
15 #include <memory>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 
20 struct B
21 {
22     static int count;
23 
BB24     B() {++count;}
BB25     B(const B&) {++count;}
~BB26     virtual ~B() {--count;}
27 };
28 
29 int B::count = 0;
30 
31 struct A
32     : public B
33 {
34     static int count;
35 
AA36     A() {++count;}
AA37     A(const A& other) : B(other) {++count;}
~AA38     ~A() {--count;}
39 };
40 
41 int A::count = 0;
42 
main(int,char **)43 int main(int, char**)
44 {
45 #ifndef TEST_HAS_NO_EXCEPTIONS
46     {
47         std::weak_ptr<A> wp;
48         try
49         {
50             std::shared_ptr<A> sp(wp);
51             assert(false);
52         }
53         catch (std::bad_weak_ptr&)
54         {
55         }
56         assert(A::count == 0);
57     }
58 #endif
59     {
60         std::shared_ptr<A> sp0(new A);
61         std::weak_ptr<A> wp(sp0);
62         std::shared_ptr<A> sp(wp);
63         assert(sp.use_count() == 2);
64         assert(sp.get() == sp0.get());
65         assert(A::count == 1);
66     }
67     assert(A::count == 0);
68     {
69         std::shared_ptr<A const> sp0(new A);
70         std::weak_ptr<A const> wp(sp0);
71         std::shared_ptr<A const> sp(wp);
72         assert(sp.use_count() == 2);
73         assert(sp.get() == sp0.get());
74         assert(A::count == 1);
75     }
76     assert(A::count == 0);
77 #ifndef TEST_HAS_NO_EXCEPTIONS
78     {
79         std::shared_ptr<A> sp0(new A);
80         std::weak_ptr<A> wp(sp0);
81         sp0.reset();
82         try
83         {
84             std::shared_ptr<A> sp(wp);
85             assert(false);
86         }
87         catch (std::bad_weak_ptr&)
88         {
89         }
90     }
91     assert(A::count == 0);
92 #endif
93 
94 #if TEST_STD_VER > 14
95     {
96         std::shared_ptr<A[]> sp0(new A[8]);
97         std::weak_ptr<A[]> wp(sp0);
98         std::shared_ptr<const A[]> sp(wp);
99         assert(sp.use_count() == 2);
100         assert(sp.get() == sp0.get());
101         assert(A::count == 8);
102     }
103     assert(A::count == 0);
104 #endif
105 
106   return 0;
107 }
108