1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <memory> 11 12 // template <class Y, class D> explicit shared_ptr(unique_ptr<Y, D>&&r); 13 14 #include <memory> 15 #include <new> 16 #include <cstdlib> 17 #include <cassert> 18 19 bool throw_next = false; 20 21 void* operator new(std::size_t s) throw(std::bad_alloc) 22 { 23 if (throw_next) 24 throw std::bad_alloc(); 25 return std::malloc(s); 26 } 27 28 void operator delete(void* p) throw() 29 { 30 std::free(p); 31 } 32 33 struct B 34 { 35 static int count; 36 37 B() {++count;} 38 B(const B&) {++count;} 39 virtual ~B() {--count;} 40 }; 41 42 int B::count = 0; 43 44 struct A 45 : public B 46 { 47 static int count; 48 49 A() {++count;} 50 A(const A&) {++count;} 51 ~A() {--count;} 52 }; 53 54 int A::count = 0; 55 56 int main() 57 { 58 { 59 std::unique_ptr<A> ptr(new A); 60 A* raw_ptr = ptr.get(); 61 std::shared_ptr<B> p(std::move(ptr)); 62 assert(A::count == 1); 63 assert(B::count == 1); 64 assert(p.use_count() == 1); 65 assert(p.get() == raw_ptr); 66 assert(ptr.get() == 0); 67 } 68 assert(A::count == 0); 69 { 70 std::unique_ptr<A> ptr(new A); 71 A* raw_ptr = ptr.get(); 72 throw_next = true; 73 try 74 { 75 std::shared_ptr<B> p(std::move(ptr)); 76 assert(false); 77 } 78 catch (...) 79 { 80 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 81 assert(A::count == 1); 82 assert(B::count == 1); 83 assert(ptr.get() == raw_ptr); 84 #else 85 assert(A::count == 0); 86 assert(B::count == 0); 87 assert(ptr.get() == 0); 88 #endif 89 } 90 } 91 assert(A::count == 0); 92 } 93