Allocator::pointer and such

by (Login ComputerGhost)
R

I think that I see a problem with my C++ compiler's include files. However, it may be just that I do not yet fully understand allocators and how they are used.

In ordered containers, the following typedefs are used (I use set as an example):

template<typename T, typename Comp=less<T>, typename Allocator=allocate<T> >
class set
{
public:
    typedef T value_type;
    typedef T* pointer;
    typedef T& reference;
...
};

First, why are those typedefs not, respectively, allocator_traits<Allocator>::value_type, allocator_traits<Allocator>::pointer, and allocator_traits<Allocator>::reference?

If you're using some funky allocator, pointer may not be T*. Unless I'm missing something in the standard, an iterator could be used as a pointer as it meets the requirements (*p = T&, etc.). I think the reference should be okay regardless of the type of "pointer", and of course, value_type is also fine. The pointer, however, may not be correct.

Furthermore, look at how a node for the set is defined:

template<typename T>
struct node
{
    typedef T value_type;
    typedef T* pointer;
    typedef T& reference;
    value_type value;
    node * parent;
    node * left;
    node * right;
    enum color_type {red, black} color;
};

That makes sense until you look at how a node is added to the set. The set does not use allocator_type to allocate its elements; instead, it uses allocator_type::rebind<node>::other. This works fine until the hypothetical situation above occurs and pointer is an iterator. The parent, left, and right members of the node become invalid!


Is there some rule somewhere that states that customAllocator.allocate(1) MUST return a type such that the following works?
T* p = allocate(1);
T& i = *p;
p = &i;







    
This message has been edited by ComputerGhost on Apr 4, 2012 5:55 PM
This message has been edited by ComputerGhost on Apr 4, 2012 5:55 PM

Posted on Apr 4, 2012, 5:54 PM

Respond to this message   

Return to Index


Response TitleAuthor and Date
I found my answer in a more up-to-date draft, and I posted my learnings on my blog. on Apr 5