Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-08-16 08:42:21


From: "Eric Woodruff" <Eric.Woodruff_at_[hidden]>
> I'd like to have the inhertance relationship added to shared_ptr. Attached
> is a version of shared_ptr.hpp with the simple modification made.

Eric,

there is no inheritance relationship between pointers. Neither should there
be for shared_ptr's.

When Derived inherits from (IS-A) Base, there is a standard conversion from
Derived* to Base*, but Derived* does not inherit from (IS-NOT-A) Base*. If
it did, there would be a conversion from Derived** to Base**, and there
isn't.

The "throw by pointer" paradigm is obsolete. Its main usefulness was that
the exception object, typically allocated via "new", was guaranteed to be
immune from stack unwinding, and therefore, it was possible to use a
TRY/CATCH macro/longjmp replacement on compilers that didn't implement the
real thing.

Real exception handling doesn't need this idiom.

Here is a library solution to the problem that requires no paradigm changes,
and is less intrusive:

struct transportable_exception
{
    virtual transportable_exception* clone() const = 0;
    virtual void throw_this() const = 0;
};

template<class E> struct wrapped_exception: public E, public
transportable_exception
{
    transportable_exception(E const & e): E(e) {}

    virtual transportable_exception* clone() const
    {
        return new wrapped_exception(*this);
    }

    virtual void throw_this() const
    {
        throw *this;
    }
};

template<class E> wrapped_exception<E> make_transportable_exception(E const
& e)
{
    return wrapped_exception<E>(e);
}

// Old user code

throw std::logic_error();

// ...

catch(std::exception const & e)

// New user code

throw make_transportable_exception(std::logic_error());

// ...

// catch clauses unchanged


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