Boost logo

Boost :

From: Jonathan Wakely (cow_at_[hidden])
Date: 2005-02-28 08:18:38

On Mon, Feb 28, 2005 at 08:24:42AM +0000, Jonathan Wakely wrote:

> I might be wrong, but does 12.5/4 mean it is ill-formed to call delete
> on a pointer-to-base if base::~base is not virtual?

It's undefined ...

12.5 []

    -5- When a delete-expression is executed, the selected deallocation
    function shall be called with the address of the block of storage to
    be reclaimed as its first argument and (if the two-parameter style
    is used) the size of the block as its second argument.*

        [Footnote: If the static type in the delete-expression is
        different from the dynamic type and the destructor is not
        virtual the size might be incorrect, but that case is already
        undefined; see expr.delete. --- end foonote]

5.3.5 [expr.delete]

    -3- In the first alternative (delete object), if the static type of
    the operand is different from its dynamic type, the static type
    shall be a base class of the operand's dynamic type and the static
    type shall have a virtual destructor or the behavior is undefined.
    In the second alternative (delete array) if the dynamic type of the
    object to be deleted differs from its static type, the behavior is

In other words, you cannot guarantee substitutability if a base type
does not have a virtual dtor and derived objects will be created on the
free store and destroyed via pointers to base - however trivial the
derived destructor.

Therefore this program does invoke undefined behaviour:

    #include <boost/date_time/posix_time/posix_time.hpp>

    int main()
        using namespace boost::posix_time;
        time_duration* t = new seconds(5);
        delete t;


Boost list run by bdawes at, gregod at, cpdaniel at, john at