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()(A* 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::unique_ptr<A> u(new A); 45 A* const uPointee = u.get(); 46 std::shared_ptr s = std::move(u); 47 ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>); 48 assert(u == nullptr); 49 assert(s.get() == uPointee); 50 } 51 { 52 std::unique_ptr<A, D> u(new A, D{}); 53 A* const uPointee = u.get(); 54 std::shared_ptr s(std::move(u)); 55 ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>); 56 assert(u == nullptr); 57 assert(s.get() == uPointee); 58 } 59 60 return 0; 61 } 62