Boost logo

Boost :

Subject: Re: [boost] Interest in Boost.Array companion: StaticVector - A statically allocated fixed capacity vector
From: Andrew Hundt (athundt_at_[hidden])
Date: 2011-08-15 17:05:58


>
> 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).
>

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.

>
> * 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.
>

Are you suggesting I simply remove the assert? I know that vectors do not
check the size, but BOOST_ASSERT should only be called when compiled in
debug mode. I chose to detect when users are accessing what is now
uninitialized memory in debug mode, with no effect on release mode. If the
convention is different I can remove the assert.

Thanks for the help!

Cheers!
Andrew Hundt

On Sun, Aug 14, 2011 at 1:11 PM, Marshall Clow <mclow.lists_at_[hidden]>wrote:

> 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
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


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