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 20 #include "test_macros.h" 21 22 struct A {}; 23 24 struct D { 25 void operator()(A* ptr) const 26 { 27 delete ptr; 28 } 29 }; 30 31 int main(int, char**) 32 { 33 { 34 std::shared_ptr<A> s0(new A); 35 std::weak_ptr<A> w = s0; 36 auto s = std::shared_ptr(w); 37 ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>); 38 assert(s0.use_count() == 2); 39 assert(s.use_count() == 2); 40 assert(s0.get() == s.get()); 41 } 42 { 43 std::unique_ptr<A> u(new A); 44 A* const uPointee = u.get(); 45 std::shared_ptr s = std::move(u); 46 ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>); 47 assert(u == nullptr); 48 assert(s.get() == uPointee); 49 } 50 { 51 std::unique_ptr<A, D> u(new A, D{}); 52 A* const uPointee = u.get(); 53 std::shared_ptr s(std::move(u)); 54 ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>); 55 assert(u == nullptr); 56 assert(s.get() == uPointee); 57 } 58 59 return 0; 60 } 61