Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2008-07-15 10:20:36


On Jul 15, 2008, at 3:28 AM, Johan Torp wrote:

> So scoped_ptr's type tells us something and there is probably lots
> of code
> out there which wants to convey this information. Therefor we
> shouldn't
> change it's semantics. Also, scoped_ptr and auto_ptr are more
> lightweight
> since they do not have the extra level of indirection that a custom
> deleter
> requires.

I agree that scoped_ptr provides semantics that unique_ptr doesn't and
so should not be removed from boost. However I did want to clarify
one bit: sizeof(unique_ptr<int>) == sizeof(auto_ptr<int>) ==
sizeof(scoped_ptr<int>) == sizeof(int*). And unique_ptr<int> doesn't
allocate any extra memory on the heap (unlike shared_ptr, and just
like scoped_ptr and auto_ptr).

The deleter of unique_ptr is a "static deleter". This is in contrast
to shared_ptr's "dynamic deleter". The static deleter requires no
overhead unless it contains state (the default_delete is stateless).

Simplified:

template <class T>
class scoped_ptr
{
     T* ptr_;
public:
     explicit scoped_ptr(T* p) : ptr_(p) {}
     ...
};

template <class T, class D = default_delete<T>>
class unique_ptr
{
     compressed_pair<T*, D> ptr_; // or a future implementation of
tuple<T*, D> ! ;-)
public:
     explicit unique_ptr(T* p) : ptr_(p) {}
     unique_ptr(T* p, D&& d) : ptr_(p, std::move(d)) {}
     ...
};

template <class T>
class shared_ptr
{
     __shared_ptr_base* ptr_;
public:
     template <class Y>
        explicit shared_ptr(Y* p) : ptr_(new __shared_ptr_imp<Y,
default_delete<Y>>(p)) {}
     template <class Y, class D>
        shared_ptr(Y* p, D d) : ptr_(new __shared_ptr_imp<Y, D>(p, d))
{}
     ...
};

-Howard


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