Boost logo

Boost :

Subject: Re: [boost] Interest in StaticVector - fixed capacity vector
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2011-10-12 18:52:15


On Oct 9, 2011, at 1:59 PM, Andrew Hundt wrote:

> 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:
>> 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?
>>
>> Cheers!
>> Andrew Hundt
>

Fwiw, one can create allocators that allocate off the stack and then turn any container into a static_container. Here's an example vector, list and set: http://home.roadrunner.com/~hinnant/stack_alloc.html

This particular implementation will spill to heap if the static buffer is overrun. But you could easily build another allocator that had another behavior. Just doing an allocator and res-using existing allocator-aware containers (e.g. std::vector) seems like a win to me.

Howard


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