|
Boost : |
From: Howard Hinnant (hinnant_at_[hidden])
Date: 1999-11-23 20:08:50
I've done enough investigating that I've convinced myself that a
"auto_ptr" that can handle standard conforming allocators would be useful
in the implementation of at least std::vector. I haven't implemented a
complete vector, but parts of one. The code is coming out very similar
to that proposed in:
http://www.research.att.com/~bs/3rd_safe0.html
But instead of relying on vector_base, vector has members that look like:
allocated_ptr<T, Allocator> data_;
size_type size_;
data_ contains the allocator, the pointer to the data, and the capacity.
Priliminary investigation is suggesting that a vector based on
allocated_ptr is neither better nor worse than one based on vector_base.
I did add one more constructor to allocated_ptr:
allocated_ptr(size_type cap, const allocator_type& a)
: base(0, a), capacity_(cap) {if (capacity_ > 0) ptr() =
allocator().allocate(capacity_);}
Naturally one could only instantiate this constructor with a full fledged
allocator (as opposed to just a Deleter).
This allowed an example vector constructor to simplify to:
explicit vector(size_type n, const T& value = T(), const Allocator& a =
allocator_type())
: data_(n, a),
size_(n)
{
uninitialized_fill_n(data_.get(), size_, value);
}
which is very similar to that proposed in Bjarne's draft appendix.
Essentially allocated_ptr is the same thing as vector_base, only
conceptually generalized, and is now a member instead of a base class.
If there is any advantage, it's that allocated_ptr is more reusable
(perhaps for deque or other non standard sequences that are based on
std::allocator), and has the empty member optimization built in.
sizeof(vector) for this experiment is 3 words.
I'm not completely happy with the name allocated_ptr. This animal
contains the allocator, the pointer, and the capacity, which is
everything needed to deallocate the pointer with a standard conforming
allocator. With the exception of accessors for the allocator and
capacity, it acts pretty much like a pointer. allocated_ptr isn't bad,
but I'm of course open to suggestions.
-Howard
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk