183564056SLogan Smith //===----------------------------------------------------------------------===//
283564056SLogan Smith //
383564056SLogan Smith // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
483564056SLogan Smith // See https://llvm.org/LICENSE.txt for license information.
583564056SLogan Smith // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
683564056SLogan Smith //
783564056SLogan Smith //===----------------------------------------------------------------------===//
883564056SLogan Smith 
983564056SLogan Smith // <memory>
1031cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
1183564056SLogan Smith 
1283564056SLogan Smith // template<class T> class shared_ptr
1383564056SLogan Smith 
1483564056SLogan Smith // shared_ptr(weak_ptr<T>) -> shared_ptr<T>
1583564056SLogan Smith // shared_ptr(unique_ptr<T>) -> shared_ptr<T>
1683564056SLogan Smith 
1783564056SLogan Smith #include <memory>
1883564056SLogan Smith #include <cassert>
1983564056SLogan Smith 
2083564056SLogan Smith #include "test_macros.h"
2183564056SLogan Smith 
2283564056SLogan Smith struct A {};
2383564056SLogan Smith 
2483564056SLogan Smith struct D {
operator ()D25*6b9e0af8SLouis Dionne   void operator()(A const* ptr) const
26afc8b497Szoecarver   {
27afc8b497Szoecarver     delete ptr;
28afc8b497Szoecarver   }
2983564056SLogan Smith };
3083564056SLogan Smith 
main(int,char **)3183564056SLogan Smith int main(int, char**)
3283564056SLogan Smith {
3383564056SLogan Smith   {
3483564056SLogan Smith     std::shared_ptr<A> s0(new A);
3583564056SLogan Smith     std::weak_ptr<A> w = s0;
3683564056SLogan Smith     auto s = std::shared_ptr(w);
3783564056SLogan Smith     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
3883564056SLogan Smith     assert(s0.use_count() == 2);
3983564056SLogan Smith     assert(s.use_count() == 2);
4083564056SLogan Smith     assert(s0.get() == s.get());
4183564056SLogan Smith   }
4283564056SLogan Smith   {
43*6b9e0af8SLouis Dionne     std::shared_ptr<A const> s0(new A);
44*6b9e0af8SLouis Dionne     std::weak_ptr<A const> w = s0;
45*6b9e0af8SLouis Dionne     auto s = std::shared_ptr(w);
46*6b9e0af8SLouis Dionne     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A const>);
47*6b9e0af8SLouis Dionne     assert(s0.use_count() == 2);
48*6b9e0af8SLouis Dionne     assert(s.use_count() == 2);
49*6b9e0af8SLouis Dionne     assert(s0.get() == s.get());
50*6b9e0af8SLouis Dionne   }
51*6b9e0af8SLouis Dionne 
52*6b9e0af8SLouis Dionne   {
5383564056SLogan Smith     std::unique_ptr<A> u(new A);
54*6b9e0af8SLouis Dionne     A* uPointee = u.get();
5583564056SLogan Smith     std::shared_ptr s = std::move(u);
5683564056SLogan Smith     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
5783564056SLogan Smith     assert(u == nullptr);
5883564056SLogan Smith     assert(s.get() == uPointee);
5983564056SLogan Smith   }
6083564056SLogan Smith   {
61*6b9e0af8SLouis Dionne     std::unique_ptr<A const> u(new A);
62*6b9e0af8SLouis Dionne     A const* uPointee = u.get();
63*6b9e0af8SLouis Dionne     std::shared_ptr s = std::move(u);
64*6b9e0af8SLouis Dionne     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A const>);
65*6b9e0af8SLouis Dionne     assert(u == nullptr);
66*6b9e0af8SLouis Dionne     assert(s.get() == uPointee);
67*6b9e0af8SLouis Dionne   }
68*6b9e0af8SLouis Dionne 
69*6b9e0af8SLouis Dionne   {
7083564056SLogan Smith     std::unique_ptr<A, D> u(new A, D{});
71*6b9e0af8SLouis Dionne     A* uPointee = u.get();
7283564056SLogan Smith     std::shared_ptr s(std::move(u));
7383564056SLogan Smith     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
7483564056SLogan Smith     assert(u == nullptr);
7583564056SLogan Smith     assert(s.get() == uPointee);
7683564056SLogan Smith   }
77*6b9e0af8SLouis Dionne   {
78*6b9e0af8SLouis Dionne     std::unique_ptr<A const, D> u(new A, D{});
79*6b9e0af8SLouis Dionne     A const* uPointee = u.get();
80*6b9e0af8SLouis Dionne     std::shared_ptr s(std::move(u));
81*6b9e0af8SLouis Dionne     ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A const>);
82*6b9e0af8SLouis Dionne     assert(u == nullptr);
83*6b9e0af8SLouis Dionne     assert(s.get() == uPointee);
84*6b9e0af8SLouis Dionne   }
8583564056SLogan Smith 
8683564056SLogan Smith   return 0;
8783564056SLogan Smith }
88