Boost logo

Boost :

Subject: Re: [boost] Interest in StaticVector - fixed capacity vector
From: Peter Dimov (pdimov_at_[hidden])
Date: 2011-10-15 16:21:44


Dave Abrahams wrote:
> on Sat Oct 15 2011, "Peter Dimov" <pdimov-AT-pdimov.com> wrote:
> > Yes, it's trivial to argue that logic errors should not be exceptions,
> > but why is push_back over capacity "almost always" a logic error?
>
> Because for me it is like writing past the end of an array, and my
> intuition tells me that it will be like that for many other people.

It's not. Writing past the end, meaning size(), of a static_vector is like
writing past the end of an array. push_back into a static_vector is like
push_back into a vector.

> *Generally speaking*, in programming, if you ask for something with a
> fixed capacity you had better not exceed it if you want your program to
> achieve the intended result.

If you use static_vector<char,...> as you would a char[], then yes, I guess.
It's not clear what you'd gain from it though, as the whole point of not
using char[] is to eliminate the buffer overflows.

> > Heck... why is it a logic error at all, except in the trivial case in
> > which you start with an empty static_vector<T, N> and do exactly N
> > push_backs,
>
> ? That's not a logic error. You're not making sense to me.

Going over capacity would be a logic error in this case, because you're not
supposed to; the number of push_backs is known at compile time. If run time
doesn't agree, you have a bug.

Anyway. Do you agree that the number of push_backs is typically a run time
value?


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