Boost logo

Boost :

Subject: Re: [boost] Interest in Boost.Array companion: StaticVector - A statically allocated fixed capacity vector
From: Marshall Clow (mclow.lists_at_[hidden])
Date: 2011-08-14 13:11:04


On Aug 14, 2011, at 2:55 AM, Andrew Hundt wrote:

> I've implemented a small companion class to boost.array that functions like
> a stack allocated vector with fixed capacity. The motivation for this class
> came when I was using boost.array in an interprocess library, when I
> realized that I actually desired an adjustable size boost.array, without the
> complexity of the vector class in the interprocess library. The result is
> StaticVector, which is boost.array directly modified with a size in front of
> the array, and added facilities to match std::vector.
>
> The Implementation is available at:
> https://github.com/ahundt/Boost.StaticVector
>
> Sample Code:
> StaticVector<std::size_t,3> three;
> three.push_back(5);
> three.push_back(2); // size: 2 capacity: 3
> three.push_back(3);
>
> three.push_back(1); // throws std::out_of_range exception indicating the
> capacity has been exceeded
>
> So here is the big question:
> Is there any interest in the class?

I took a look at the class, and I have a couple of comments:

* Since the underlying storage is "T elems[N]", this means that N different elements get created at construction time. While this is fine for a fixed size array, a variable sized array should start out with no constructions, and create elements as they are added (like vector).

* In operator [] (line 234 and 240), you are asserting if the index is out of range. Again, this may be correct for a fixed size array, but not in a variable sized one. Compare with vector. Also, the assertion is incorrect. Consider the case where N = 10, size = 4, and i = 7.

I like the idea, but I think you need to revisit how the class works.

-- Marshall

Marshall Clow Idio Software <mailto:mclow.lists_at_[hidden]>

A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
        -- Yu Suzuki


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk