pointer_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// template<class Y> explicit shared_ptr(Y* p); 13 14#include <memory> 15#include <new> 16#include <cstdlib> 17#include <cassert> 18 19struct A 20{ 21 static int count; 22 23 A() {++count;} 24 A(const A&) {++count;} 25 ~A() {--count;} 26}; 27 28int A::count = 0; 29 30bool throw_next = false; 31 32void* 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 39void operator delete(void* p) throw() 40{ 41 std::free(p); 42} 43 44int 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