Boost logo

Boost :

From: Eric Woodruff (Eric.Woodruff_at_[hidden])
Date: 2002-09-12 13:07:48


Peter, I've tried your wrapped_exception suggestion, and I just wanted to
comment that the wrapped_exception class has to be specialized for integtral
types to throw the value, not *this. Another reason is that one cannot
derived from an int.

Even so, the wrapped_exception works if thrown in a thread, but outside it
cannot :(

try {
   throw wrapped_exception<int> (5);
}
catch (int) {
 /// not possible
}

How about this... boost should deprecate all integral types and write
template to wrap them, with the requirement that they provide no overhead
compared to the base types. The duality between integral types versus
classes is just silly. Someone should address it, putting these wrappers in
the global namespace even.

Sure, I can address this myself, as a personal issue, but boost could
support it all the way down to numeric_cast, etc, making it far more useful.

----- Original Message -----
From: Peter Dimov
Newsgroups: gmane.comp.lib.boost.devel
Sent: Friday, 2002:August:16 9:42
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