|
Boost : |
From: Beman Dawes (bdawes_at_[hidden])
Date: 2002-05-01 15:22:04
At 10:51 PM 4/30/2002, Gennadiy Rozental wrote:
>"Peter Dimov" <pdimov_at_[hidden]> wrote in message
>news:00f801c1f068$0c7fc650$1d00a8c0_at_pdimov2...
>
>>
>> X is defined in X.hpp, along with the associated traits
specializations.
>The
>> problem, with a smart_ptr that uses the type of its template parameter
to
>> somehow alter its type (via traits, sizeof(expression-involving-X), or
>some
>> other mechanism), is that a.cpp sees a different smart_ptr<X>. So
there's
>an
>> ODR violation.
>>
>> Note that shared_ptr doesn't have this problem.
>>
>
>And how shared_ptr will automatically switch between
intrusive/nonintrusive
>counting?
Peter has figured out something really clever. Here was his description:
> What I have in mind is:
>
> * define template<class T> class intrusive_ptr that depends on
> T::add_ref
> and T::release (or, better yet, as Dave Harris pointed out, on
> add_ref(T *)
> and release(T *).) It doesn't require a common base class (per itself.)
>
> * add a shared_ptr<T>(intrusive_ptr<U> const & p) constructor that
> initializes px to p.get() and pn to p.get() implicitly converted to
> counted_base*.
>
> Voila, instant interoperability when U is derived from counted_base.
So at virtually no cost (the only change was the addition of a single
constructor, which isn't used if not needed) he has allowed shared_ptr's to
be constructed from invasive_ptr's. A marked improvement in
interoperability, particularly as needed in library interfaces.
If that isn't cool, I don't know what is.
Hopefully a policy-based smart pointers could also deliver an
invasive->shared constructor.
--Beman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk