[cpp-threads] Update on N2889/N2880/N2901

Howard Hinnant howard.hinnant at gmail.com
Tue Jun 23 00:25:56 BST 2009


On Jun 22, 2009, at 6:47 PM, Boehm, Hans wrote:

> Shared_ptr has a constructor
>
> template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
>
> However, since the allocator type A is not part of the shared_ptr  
> type, how does this work?  Assuming I construct one with a non- 
> default allocator type but, say, no interesting allocator state a,  
> how does shared_ptr know how to deallocate the reference count on  
> destruction of the final pointer to an object?
>
> This seems strange.  I'm either missing something, or we have  
> another bug in the draft here.

It stores the (possibly stateful) allocator right next to the  
(possibly stateful) deleter, and the Y* (not T*).  This is done by  
having a:

class control_block_base
{
public:
     virtual void delete_it() = 0;
};

template <class Y, class D, class A>
class control_block
    : public control_block_base
{
     Y y_;
     D d_;
     A a_;
    ...
     virtual void delete_it();  // delete y_ using d_ and deallocate  
using a_
};


template <class T>
class shared_ptr
{
     control_block_base* ptr_;
     ...
     template <class Y, class D, class A>
     shared_ptr(Y y, D d, A a)
        : ptr_(new control_block<Y, D, A>(y, d, a)) {}

     ~ shared_ptr() {ptr_->delete_it();}
};

(or something along those lines; I've ignored some important details).

-Howard




More information about the cpp-threads mailing list