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 | // UNSUPPORTED: c++03, c++11, c++14 |
11 | |
12 | // template<class T> class shared_ptr |
13 | |
14 | // shared_ptr(weak_ptr<T>) -> shared_ptr<T> |
15 | // shared_ptr(unique_ptr<T>) -> shared_ptr<T> |
16 | |
17 | #include <memory> |
18 | #include <cassert> |
19 | #include <utility> |
20 | |
21 | #include "test_macros.h" |
22 | |
23 | struct A {}; |
24 | |
25 | struct D { |
26 | void operator()(A const* ptr) const |
27 | { |
28 | delete ptr; |
29 | } |
30 | }; |
31 | |
32 | int main(int, char**) |
33 | { |
34 | { |
35 | std::shared_ptr<A> s0(new A); |
36 | std::weak_ptr<A> w = s0; |
37 | auto s = std::shared_ptr(w); |
38 | ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>); |
39 | assert(s0.use_count() == 2); |
40 | assert(s.use_count() == 2); |
41 | assert(s0.get() == s.get()); |
42 | } |
43 | { |
44 | std::shared_ptr<A const> s0(new A); |
45 | std::weak_ptr<A const> w = s0; |
46 | auto s = std::shared_ptr(w); |
47 | ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A const>); |
48 | assert(s0.use_count() == 2); |
49 | assert(s.use_count() == 2); |
50 | assert(s0.get() == s.get()); |
51 | } |
52 | |
53 | { |
54 | std::unique_ptr<A> u(new A); |
55 | A* uPointee = u.get(); |
56 | std::shared_ptr s = std::move(u); |
57 | ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>); |
58 | assert(u == nullptr); |
59 | assert(s.get() == uPointee); |
60 | } |
61 | { |
62 | std::unique_ptr<A const> u(new A); |
63 | A const* uPointee = u.get(); |
64 | std::shared_ptr s = std::move(u); |
65 | ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A const>); |
66 | assert(u == nullptr); |
67 | assert(s.get() == uPointee); |
68 | } |
69 | |
70 | { |
71 | std::unique_ptr<A, D> u(new A, D{}); |
72 | A* uPointee = u.get(); |
73 | std::shared_ptr s(std::move(u)); |
74 | ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>); |
75 | assert(u == nullptr); |
76 | assert(s.get() == uPointee); |
77 | } |
78 | { |
79 | std::unique_ptr<A const, D> u(new A, D{}); |
80 | A const* uPointee = u.get(); |
81 | std::shared_ptr s(std::move(u)); |
82 | ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A const>); |
83 | assert(u == nullptr); |
84 | assert(s.get() == uPointee); |
85 | } |
86 | |
87 | return 0; |
88 | } |
89 | |