Listing 7
template <class T> class DeletableSingleton { public: static void ScheduleDestruction(T*, void (*pFun)()) { static bool firstPass = true; isDead = false; deleter = pFun; if (firstPass) { std::atexit(GracefulDelete); firstPass = false; } } static void OnDeadReference() { } static void GracefulDelete(); protected: static void (*deleter)(); static bool isDead; }; template <class T> void (*DeletableSingleton<T>::deleter)(); template <class T> bool DeletableSingleton<T>::isDead = true; template< class T > void DeletableSingleton<T>::GracefulDelete() { if (isDead) return; isDead = true; deleter(); }