Topic: [Q] How does rebind work?
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/04/06 Raw View
Joe Chan wrote:
>
> Hi all,
>
> I keep seeing a thing called "rebind" showing up all over the place in
> the standard library. Can anyone explain to me how it is suppose to
> work? May be some example will help too.
Every allocator has a type of object which it is responsible for
allocating. If you want to allocate a different type of object using a
corresponding allocator type, you use rebind to get it.
template <class T, class Allocator=std::allocator<T> >
// Allocator is the type of an allocator that allocates T objects.
class myContainer
{
private:
struct node{
T x;
node *next;
node *prev;
};
typedef typename Allocator::rebind<node>::other node_allocator;
// node_allocator is a type of allocator that allocates
// myContainer::node objects.
node_allocator nalloc;
typename node_allocator::pointer node_list;
public:
myContainer(Allocator& alloc=Allocator()) nalloc(alloc) {}
Allocator get_allocator() { return nalloc; }
// Lots more details.
};
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: firstian@nospam.bellatlantic.net (Joe Chan)
Date: 2000/04/06 Raw View
Hi all,
I keep seeing a thing called "rebind" showing up all over the place in
the standard library. Can anyone explain to me how it is suppose to
work? May be some example will help too.
--
Joe Chan
Remove "nospam" to get my address.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]