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> explicit shared_ptr(Y* p); 13 14 #include <memory> 15 #include <new> 16 #include <cstdlib> 17 #include <cassert> 18 19 struct A 20 { 21 static int count; 22 23 A() {++count;} 24 A(const A&) {++count;} 25 ~A() {--count;} 26 }; 27 28 int A::count = 0; 29 30 bool throw_next = false; 31 32 void* operator new(std::size_t s) throw(std::bad_alloc) 33 { 34 if (throw_next) 35 throw std::bad_alloc(); 36 return std::malloc(s); 37 } 38 39 void operator delete(void* p) throw() 40 { 41 std::free(p); 42 } 43 44 int main() 45 { 46 { 47 A* ptr = new A; 48 throw_next = true; 49 assert(A::count == 1); 50 try 51 { 52 std::shared_ptr<A> p(ptr); 53 assert(false); 54 } 55 catch (std::bad_alloc&) 56 { 57 assert(A::count == 0); 58 } 59 } 60 } 61