General Allocator Regarding type definitions and void * specialized
problem
Hello all C++ expert programmer,
i have wrote partial general allocator for my container.
After reading standard C++ library and code guru article, i have
several questions.
1. Why allocator write forward declaration then allocation for void*
rather than directly wrote allocator first ?
[code]
namespace std {
template <class T> class allocator;
// specialize for void:
template <> class allocator<void> {
public:
typedef void* pointer;
typedef const void* const_pointer;
// reference to void members are impossible.
typedef void value_type;
template <class U> struct rebind { typedef allocator<U>
other; };
};
template <class T> class allocator {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template <class U> struct rebind { typedef allocator<U>
other; };
allocator() throw();
allocator(const allocator&) throw();
template <class U> allocator(const allocator<U>&) throw();
~allocator() throw();
pointer address(reference x) const;
const_pointer address(const_reference x) const;
pointer allocate(size_type,
allocator<void>::const_pointer hint = 0);
void deallocate(pointer p, size_type n);
size_type max_size() const throw();
void construct(pointer p, const T& val);
void destroy(pointer p);
};
}
[/code]
2. What is the use/purpose of type definition ?
[quote]
1. A couple of type definitions. These ensure that the allocators'
client (for instance, 'std::vector') is able to use some relevant
types by known names. For example, consider that you write an
allocator, that is able to allocate memory in a far area, that cannot
be reached by normal pointers (let your imagination wander). Now, the
'allocator' will use some pointer-like construct. The allocators'
client has, of course, no idea of such a thing. When a client needs to
pass such a pointer it will use the
typedef T* pointer;
and if it needs to suptract such pointers, the result will have the
type 'difference_type', whatever that internally means for the
allocator.
[/quote]
What i understand from here is because different container needs
different pointer construct memory, therefore, there are different
member data in list and vector. So, allocator need to match back its
member data with vector for example.
[quote]
typedef T* pointer; ----------- Allocator
Vector
typedef A allocator_type; typedef typename A::pointer pointer;
[/quote]
Although, i not able to understand what this does
[quote]
vector
typedef A allocator_type; typedef typename A::pointer pointer;
[/quote]
3. How allocator know when it need rebind for its allocator
client(vector, list, map, set) ?
[Quote]
This is the magic required for std::list to work properly,
since given std::list<int> ( allocator<int>() ), std::list actually
needs to allocate memory for Node<int>, and not int.
Thus, they need to rebind to allocator<int>()::rebind<Node<int> >
:ther instead.
[/quote]
[code]
template <class U>
struct rebind
{
typedef allocator<U> other;
};
list<int>;
interprete by compiler as list<int, allocator<int> >;
This code template <class T>
class allocator; is replace with allocator<int> >;
In other words, T(represent generic types) = int;
Then allocator class receive integer as argument
How compiler interpreter
template <class U>
struct rebind
{
typedef allocato<U> other;
}
How list is pass U ?
[/code]
Thanks for your help.
Please help me.
I really appreciated any help.