Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2002-10-04 00:28:49


"Eric Woodruff" <Eric.Woodruff_at_[hidden]> wrote in message
news:anj6ra$ga3$1_at_main.gmane.org...
> With class B : public A, I wanted shared_ptr<B> to derive from
> shared_ptr<A> in the past, with the idea that the share_ptr conversions
> should completely match pointer conversions (that would include const).
> Nobody went for that then, but I was wanting to throw shared_ptrs and
> catch them in the correct hierarchy, which was a very isolated case, not
> enough to justify anything.

Well, it seems to me that one potential problem with your scheme is
that:

class C : public A, public B { };

would introduce a lot of complexity. I mean, how many base classes
do you support? Maybe the preprocessor library could help, but it seems
like a pretty tricky task. I guess the next best solution is to explicitly
cast to the base type using shared_static_cast(). I wonder if this
technique
would work:

template <typename T>
class ptr
{
public:
    template <typename U>
    operator ptr<U>() const { return shared_static_cast<U>(*this); }
};

At least that would make certain contexts work properly, like so (I think):

ptr<base> p;
p = ptr<derived>();

For your throwing context, you could force the conversion like so:

void throw_ptr(ptr<base> const& p)
{
    throw p;
}

Or, I might just be blowing hot air. I'm not entirely sure, really. ;) Of
course, for the implicit-conversions-are-evil camp, you could forego
the conversion, and try this:

template <typename T>
void throw_ptr(ptr<T> const& p)
{
    throw shared_static_cast<base>(p);
}

Or, more generically:

template <typename T, typename U>
void throw_ptr(ptr<U> const& p)
{
    throw shared_static_cast<T>(p);
}

Dave


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