Boost logo

Boost :

From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2007-09-22 06:22:33


Larry Evans wrote:
> BTW, I'm trying to do std::list<T> correctly; however,
> I'm having problems. It appears allocator::pointer
> doesn't help when using:
>
> struct node
> {
> T elem;
> node* next_;
> node* prev_;
> };
>
> because the begin/end pointers are to node* not to T*.
>
That's what the rebind mechanism is for. Suppose your allocator
parameter is Alloc:

template<typename T, typename Alloc = std::allocator<T> >
class list
{
};

Then first you need the node allocator:

typedef typename Alloc::template rebind<node<T> >::other node_allocator;

Then you can get the new allocator's pointer type:

typedef typename node_allocator::pointer node_ptr;

class node
{
  T elem;
  node_ptr next;
  node_ptr prev;
};

Don't forget to construct the node_allocator member you'll hold from the
allocator you're passed in. All allocators must have a templated
constructor for this purpose.
Something most implementations do, by the way, is that they are written
in such a way that EBO applies to the allocator if it has no state.

Sebastian Redl


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