Boost logo

Boost :

Subject: [boost] Interest in StaticVector - fixed capacity vector
From: Andrew Hundt (athundt_at_[hidden])
Date: 2011-10-09 13:59:51

On Sun, Aug 14, 2011 at 5:55 AM, Andrew Hundt <athundt_at_[hidden]> 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:
> 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?
> Cheers!
> Andrew Hundt
On Mon, Aug 15, 2011 at 5:20 PM, Nevin Liber <nevin_at_[hidden]> wrote:

> On 15 August 2011 16:05, Andrew Hundt <athundt_at_[hidden]> wrote:
> > Good point. I've modified elems to be a char array, and I now
> > reinterpret_cast to the class T as necessary. Elements should only be
> > constructed when they are added now.
> >
> What are you doing about alignment? Take a look at Synyhesizing Types with
> Specific Alignments in the Type Traits library for a start.
> --
> Nevin ":-)" Liber <mailto:nevin_at_[hidden]> (847) 691-1404

Phil Endecott wrote:

> In the case of StativVector<char,N> where N<256, you should consider using
> uint8_t to store the size - and similar variations. Boost.Integer makes it
> possible to select a suitable type.

Sorry for the duplicate email, but my other email seemed to get lost in the
shuffle. I know its been a while since I sent my first emails regarding
StaticVector, but I have fixed some of the issues others pointed out.

- I added a benchmark based on a move benchmark found on C++Next, that shows
StaticVector outperforming vector by about 30%, which seems reasonable since
there is no dynamic allocation.
 - I believe I've resolved the StaticVector alignment using type_traits'
aligned_storage, as suggested by Nevin Liber.
 - I've also added the use of Boost.Integer to select the smallest possible
type to track the size of the vector given its capacity, as suggested by
Phil Endecott.

The Implementation is available at:

Anyone still have additional thoughts or interest in this?

Andrew Hundt

Boost list run by bdawes at, gregod at, cpdaniel at, john at