Boost logo

Boost :

Subject: Re: [boost] Interest in StaticVector - fixed capacity vector
From: Dave Abrahams (dave_at_[hidden])
Date: 2011-10-15 14:44:41


on Sat Oct 15 2011, "Peter Dimov" <pdimov-AT-pdimov.com> wrote:

> Dave Abrahams wrote:
>> Nobody likes "unefined behaviour???" But please allow me to replace
>
>> that comment with:
>>
>> "// throws an exception???"
>>
>> It's just not the most appropriate response. For those who want
>> checking, dropping into the debugger or dumping core or logging and
>> terminating would be better, and those who don't will be annoyed to pay
>> for unneeded checks when their code is correct.
>
> We (as in the authors of the example) fall in neither category of
> yours. We want the algorithm that does push_back to terminate when it
> reaches capacity. Which is exactly what it will do, without having to
> be rewritten.

This approach is not scalable or composable. If you have an algorithm
of the form:

   for (...)
      v.push_back(whatever)

then yeah, it works and you can get usable results. But as soon as your
algorithm has another step after the for loop, you have to expect that
the algorithm is terminated somewhere in the middle, and the results are
either nil (if it gives the strong guarantee) or useless (if it gives
the basic guarantee).

>> Why should the library be locked into providing what is almost
>> always a suboptimal response?
>
> It's your (and others') assertion that this is almost always a
> suboptimal response. You haven't backed it up.

If you think of this thing as a vector with a length bound, and you try
to exceed the capacity, I claim it's probably a bug. IIUC, you claim it
might just as well not be a bug. I'm not asking you to "back that up."
I'm asking you to use your noggin and think about what happens most of
the time in most of the code you see. If we say "prove it" to one
another, there's going to be a stand-off, because you know as well as I
do that nobody knows for certain.

> 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.
*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
acheieve the intended result. That's my experience. Yours may differ.
Does it? If so, I'll loosen my conviction.

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

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

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