pointer_deleter_throw.pass.cpp revision bc8d3f97eb5c958007f2713238472e0c1c8fe02c
1//===----------------------------------------------------------------------===// 2// 3// ��������������������The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10// <memory> 11 12// shared_ptr 13 14// template<class Y, class D> shared_ptr(Y* p, D d); 15 16#include <memory> 17#include <cassert> 18#include <new> 19#include <cstdlib> 20#include "../test_deleter.h" 21 22struct A 23{ 24 static int count; 25 26 A() {++count;} 27 A(const A&) {++count;} 28 ~A() {--count;} 29}; 30 31int A::count = 0; 32 33bool throw_next = false; 34 35void* 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 42void operator delete(void* p) throw() 43{ 44 std::free(p); 45} 46 47int main() 48{ 49 A* ptr = new A; 50 throw_next = true; 51 try 52 { 53 std::shared_ptr<A> p(ptr, test_deleter<A>(3)); 54 assert(false); 55 } 56 catch (std::bad_alloc&) 57 { 58 assert(A::count == 0); 59 assert(test_deleter<A>::count == 0); 60 assert(test_deleter<A>::dealloc_count == 1); 61 } 62} 63