Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-02-03 10:29:45


From: "Pavel Vasiliev" <pavel_at_[hidden]>
[...]

> Also there is a question related to boost::shared_ptr: what the
> drawbacks would be in implementing it via void*? I mean
>
> template<class T> class shared_ptr
> {
> void * px;
> detail::shared_count pn;
> ...
> public:
> T * get() const
> {
> return static_cast<T*>(px);
> }
> }
>
> The advantage is that with void*-based implementation all
> shared_ptr<>’s are layout-compatible and thus std containers may
> be specialized for shared_ptr to reduce code bloat (since not all
> linkers are as smart as MSVC++).

There are no significant drawbacks that I can see.

The only drawback is that with a void * px it's easier to create a broken
implementation that will almost work. For example

template<class Y> shared_ptr<T>::shared_ptr(shared_ptr<Y> const & rhs):
px(rhs.px), pn(rhs.pn) {}

is buggy, and should now be

template<class Y> shared_ptr<T>::shared_ptr(shared_ptr<Y> const & rhs):
px(static_cast<T*>(rhs.get())), pn(rhs.pn) {}


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