Boost logo

Boost :

From: Eric Woodruff (Eric.Woodruff_at_[hidden])
Date: 2002-08-16 09:41:28


What?

Peter, how can you catch an std::exception& and then write throw
make_transportable_exception(std::logic_error()); The type is unknown unless
we can change how it was thrown and that is not possible.

A handle to an object should be able to be supplanted for a regular object,
therefore it requires inheritance.

----- Original Message -----
From: Peter Dimov
Newsgroups: gmane.comp.lib.boost.devel
Sent: Friday, 2002:August:16 9:42 AM
Subject: Re: shared_ptr inheritance relationship

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

_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


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