
O/H Sebastian Redl έγραψε:
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.
But STL containers do not use the rebind struct for their internal allocations or for their member pointers. They just use T* instead of allocator<T>::rebind<U>::pointer.