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++98, c++03, c++11, c++14
11 // UNSUPPORTED: libcpp-no-deduction-guides
12 
13 // template<class T> class shared_ptr
14 
15 // shared_ptr(weak_ptr<T>) -> shared_ptr<T>
16 // shared_ptr(unique_ptr<T>) -> shared_ptr<T>
17 
18 #include <memory>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 
23 struct A {};
24 
25 struct D {
26   void operator()(void*) const {}
27 };
28 
29 int main(int, char**)
30 {
31   {
32     std::shared_ptr<A> s0(new A);
33     std::weak_ptr<A> w = s0;
34     auto s = std::shared_ptr(w);
35     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
36     assert(s0.use_count() == 2);
37     assert(s.use_count() == 2);
38     assert(s0.get() == s.get());
39   }
40   {
41     std::unique_ptr<A> u(new A);
42     A* const uPointee = u.get();
43     std::shared_ptr s = std::move(u);
44     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
45     assert(u == nullptr);
46     assert(s.get() == uPointee);
47   }
48   {
49     std::unique_ptr<A, D> u(new A, D{});
50     A* const uPointee = u.get();
51     std::shared_ptr s(std::move(u));
52     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
53     assert(u == nullptr);
54     assert(s.get() == uPointee);
55   }
56 
57   return 0;
58 }
59