Boost logo

Boost :

Subject: Re: [boost] [container] static_vector: fixed capacity vector update
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2013-01-18 16:17:52


On Thu, Jan 17, 2013 at 7:19 AM, Andrew Hundt <athundt_at_[hidden]> wrote:

> *static_vector is a hybrid of boost::container::vector and boost::array
> with fixed capacity.
>
> Adam Wulkiewicz and I have updated static_vector with improvements from the
> list discussion and reorganized it for possible inclusion into
> boost.container.
>

This is encouraging!

> Overview:
> static_vector is a sequence container with contiguous storage that can
> change in size like boost::container::vector. It also includes static
> allocation, low overhead, and fixed capacity similar to boost::array.
>
> Synopsis:
> template*
> *<*
> * typename Value, *
> * std::size_t Capacity, *
> * typename Strategy =
> strategy::def<
> https://svn.boost.org/svn/boost/sandbox/static_vector/doc/html/static_vector/reference.html#structboost_1_1container_1_1strategy_1_1def
> >
> <Value> *
> *>*
> *class static_vector;
>
> Example:
> // static_vector of ints, fixed capacity: 3
> boost::container::static_vector<int,3> three; // size: 0
>
> three.push_back(1); // size: 1
> three.push_back(2); // size: 2
> three.push_back(3); // size: 3
>
> //three.reserve(4); // no effect, fixed capacity: 3
> //three.push_back(3); // size: 4, undefined behavior
>
> three.pop_back(); // size: 2
> three.shrink_to_fit(); // no effect, fixed capacity: 3
>
> Documentation:
>
> https://svn.boost.org/svn/boost/sandbox/static_vector/doc/html/index.html
>
> Source:
> https://svn.boost.org/svn/boost/sandbox/static_vector/
>
> Discussions from boost.devel archive:
> http://goo.gl/PKEpB [google groups]
>

Great, seems almost simple enough. I say "almost" because, IMHO, we
shouldn't try *too* hard to make static_vector a drop-in replacement for
vector. Make the interface common where it makes sense, but reserve and
shrink_to_fit shouldn't be part of the interface. It would just confuse me
if I saw a reserve or shrink_to_fit method call on a static_vector in real
code.

Changes:
> - C++11 support
>

Please elaborate.

> - moved to boost::container namespace
> - strategy based error handling (“strategy” is aka “policy”)
>

The documentation is not the prettiest right now, which is fine, but one
key omission is a Strategy concept specification (at least, I didn't see
one).

Also, I'm not 100% sold on the Strategy template parameter. I think there's
something to be said for simplicity, and the simplest implementation would
have just 2 template parameters and assert on any bounds violations; if you
want to throw, use, say, a free function push_back_and_throw_on_error. At
least, that's what I'd say is an alternative design.

    - bounds checks are asserts by default but can be switched to exceptions
>

Is this true for static_vector::at as well, or does that throw regardless
as with std::vector?

> - memory is uninitialized until objects are inserted
>

Hmmm...this is a change?! I would think this is a correctness requirement :/

> - internal size is currently the same as Strategy::size_type
> - expanded documentation and unit tests
> - optimizations based on type traits
>

Please elaborate.

> - boost::interprocess support
>

Please elaborate.

[...snip Q&A's, design discussion...]

- Jeff


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