M
Mark A. Gibbs
I'm having yet another headache with making a standard allocator. What
behaviour should be expected from the assigment operator? When would it
be called? Why is it there at all, when you can't change allocators in
containers?
To give a little more depth, I have a class like this:
class HeapAllocator
{
// All requirements of the allocator interface are met along with:
// - A constructor to pass in a heap reference
// - A GetHeap function that returns a reference to the internal heap
// (which can never be 0)
private:
mutable Heap* heap_;
};
Now, I would have liked to define heap_ as a Heap&, but I can't because of:
template <class U>
HeapAllocator& operator=(HeapAllocator<U> const& ha) /*throw()*/
{
heap_ = ha.heap_;
}
Which is an annoyance but otherwise not that big of an issue. The
problem for me is one of elegance and robustness. Under what
circumstances could I expect operator= to be called (aside from in
operator= in a container)? Memory allocated in one heap cannot be
deallocated in another (operator== tests for heap equality). Am I
guaranteed that all allocations would be deallocated by the correct
allocator (or to put it more specifically allocations by an allocator A
will only be deallocated by A or by another allocator B for which B ==
A) - ie, is this a requirement?
I'm a little disturbed by the statement in 20.1.5.4:
"Implementations of containers described in this International Standard
are permitted to assume that their Allocator template parameter meets
the following two additional requirements....
- All instances of a given allocator type are required to be
interchangeable and always compare equal to each other."
Does this mean that I am essentially wasting my time?
mark
behaviour should be expected from the assigment operator? When would it
be called? Why is it there at all, when you can't change allocators in
containers?
To give a little more depth, I have a class like this:
class HeapAllocator
{
// All requirements of the allocator interface are met along with:
// - A constructor to pass in a heap reference
// - A GetHeap function that returns a reference to the internal heap
// (which can never be 0)
private:
mutable Heap* heap_;
};
Now, I would have liked to define heap_ as a Heap&, but I can't because of:
template <class U>
HeapAllocator& operator=(HeapAllocator<U> const& ha) /*throw()*/
{
heap_ = ha.heap_;
}
Which is an annoyance but otherwise not that big of an issue. The
problem for me is one of elegance and robustness. Under what
circumstances could I expect operator= to be called (aside from in
operator= in a container)? Memory allocated in one heap cannot be
deallocated in another (operator== tests for heap equality). Am I
guaranteed that all allocations would be deallocated by the correct
allocator (or to put it more specifically allocations by an allocator A
will only be deallocated by A or by another allocator B for which B ==
A) - ie, is this a requirement?
I'm a little disturbed by the statement in 20.1.5.4:
"Implementations of containers described in this International Standard
are permitted to assume that their Allocator template parameter meets
the following two additional requirements....
- All instances of a given allocator type are required to be
interchangeable and always compare equal to each other."
Does this mean that I am essentially wasting my time?
mark