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