Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2002-11-19 12:51:02


> Borland C++ 5.5.1 fails the following test:
>
> struct X
> {
> X(): next() {}
> boost::shared_ptr<X> next;
> };
>
> void test()
> {
> boost::shared_ptr<X> p(new X);
> p->next = boost::shared_ptr<X>(new X);
> BOOST_TEST(!p->next->next);
> p = p->next;
> BOOST_TEST(!p->next); // here
> }
>
The problem is here;

   p = p->next;

This assignment is slicing. It is directly calling

    shared_count & operator= (shared_count const & r) // nothrow

on the lvalue (p). The pointee (p.px) is destroyed during "pi_->release();"
but is not re-assigned.

If the intention was that the assignment use:

    template<typename Y>
    shared_ptr & operator=(shared_ptr<Y> const & r) // never throws

then Borland doesn't use it.

A solution is to add a non-template assignment, at least only for bcc5.5.1,
as:

    shared_ptr & operator=(shared_ptr const & r) // never throws
    {
        px = r.px;
        pn = r.pn; // shared_count::op= doesn't throw
        return *this;
    }

With this additional assignment, the test passes.

Can't check with other Borland compilers.

Fernando Cacciola


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk