template <class T>
class allocator
{
public:
...
// what is the purpose for this struct in allocator class
template <class U>
struct rebind
{
typedef allocator<U> other;
};
...
};
on this:
1 2 3 4
allocator<T> a;
T* t = a.allocate(1);
allocator<T>::rebind<N>::other na;
N* n = na.allocate(1);
any special reason they use this? as this can be written:
1 2 3 4
allocator<T> a;
T* t = a.allocate(1);
allocator<N> na;
N* n = na.allocate(1);
I just read the presentation. Nice. Let me see if I can answer.
There are containers in STL that are of type T but don't allocate objects of type T. The example in the presentation is list. list<T> allocates nodes, as in a linked list. The class of this node, however, is private (I don't see it documented in this site anywhere), so you can't really say:
So in order to solve this problem, the rebind construct is used. The rebind struct allows the std::list class to obtain an allocator of a different type to allocate the nodes.
And I think that should be it. I'm not 100% sure if the real reason is that listnode is private, or simply this is a construct devised to simplify the syntax.