boost::checked delete

From: Capstar (news_at_deleg.homeip.net)
Date: 10/01/04


Date: Fri, 01 Oct 2004 11:07:25 +0200

Hi NG,

I was looking through the code of boost::shared_ptr, and found the
following piece of code in some referenced header file. As far as I
understand it, it is used to delete the object the shared pointer object
points to.
I think the struct checked_deleter is used to store the deleter in the
shared_ptr class without having to store a function pointer.

But what I don't understand is what the purpose is of checked delete.
what additional safety does it bring?
Maybe I'm just missing the big picture, but the compiler knows or does
not know how to delete an object. So why would a typedef and a
completely useless (void)sizeof( that_typedef ) help?

Thanks in advance,
   Mark

template<class T> inline void checked_delete(T * x)
{
     // intentionally complex - simplification causes regressions
     typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
     (void) sizeof(type_must_be_complete);
     delete x;
}

template<class T> struct checked_deleter
{
     typedef void result_type;
     typedef T * argument_type;

     void operator()(T * x) const
     {
         // boost:: disables ADL
         boost::checked_delete(x);
     }
};

-- 
<<Remove the del for email>>


Relevant Pages