Boost logo

Boost :

From: David M. Jones (djones_at_[hidden])
Date: 2004-10-18 09:47:31


"Peter Dimov" <pdimov_at_[hidden]> wrote in message
news:002e01c4b37d$cfac5c80$6401a8c0_at_pdimov2...
> David M. Jones wrote:
> > I ran into a situation (see my "smart list" post on the boost users
> > group) where I wanted to create a custom allocator whose pointer
> > typedef is boost::shared_ptr. In other words, the code would look
> > something like:
> >
> > template <typename T>
> > class my_allocator
> > {
> > public:
> > typedef boost::shared_ptr<T> pointer;
> > };
> >
> > The problem with doing this is that boost::shared_ptr<T> does not
> > have the same "interface" as the equalivalent raw pointer type T*. (I
> > use the term interface in the generic programming/template sense; not
> > in the object oriented sense.) One example is that there is no
> > operator= on boost::shared_ptr that takes a raw pointer; instead
> > reset() must be used.
>
> Raw pointers don't necessarily have an operator= that takes another kind
of
> raw pointer. For example, T __near * cannot be assigned T*, only itself.
> Similarly, shared_ptr<T> cannot be assigned T*, only itself.
>
> If a component tries to assign a T* to allocator<T>::pointer, it is broken
> WRT nonstandard pointers.

Thank you for your reply, Peter.

Let me be more specific about the problem I ran into. I am using MSVC 7.1
and the implementation of the STL that comes with it. When I tried to create
and use an object of type
   std::list<int, my_allocator<int> >,
I received a compiler error because the STL code contains
   _Myhead = 0;
where _Myhead is of type
   my_allocator<node>::pointer (ie. boost::shared_ptr<node>)
and node is the class that defines the nodes in the linked list.

So the STL is not calling operator=() for a boost::shared_ptr with just any
pointer type on the right-hand side. In fact, it's not even a T* -- it is
specially setting it to null.

Are you telling me that this STL implementation is (slightly) invalid? Or
are you telling me that I should not expect boost::shared_ptr to be a valid
type for use as a custom allocator::pointer? Or both?

I have read that Scott Meyers (Effective STL; Item 10) says that custom
allocators should always have
   typedef T* pointer
but I have also read that Matt Austern (December 2000 CUJ column) says that
custom allocators can sometimes be "some pointer-like class"; it seems to me
that boost:shared_ptr is a "pointer-like class".


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