Boost logo

Boost :

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


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

> Christian Holmquist wrote:
>> On 14 October 2011 12:55, Dave Abrahams <dave_at_[hidden]> wrote:
>
>> > Is throwing an exception going to turn an incorrect program into a
>> > correct one?
>>
>> I'm probably completely misunderstanding your point of view, or rather,
>> what is your point of view?
>
> He's saying that if a program does a push_back when the capacity has
> been reached, this program has a logic error, and throwing an
                            ^
almost certainly -----------^

> exception will not make the error go away.
>
> I'm not so sure of that, because doing a push_back when the capacity
> has been reached is not necessarily a logic error if the code is well
> aware of the possibility and prepared to handle the exception.
>
> try
> {
> do some push_backs into a static_vector; we don't know how many
> }
> catch( out_of_capacity& )
> {
> // ignore, what we gathered so far is good enough for gov't use
> }

Yes, it's possible, but highly unusual, for someone to use the class
that way. And we were talking about being a drop-in replacement for
std::vector, in which case you wouldn't have coded for this kind of
possibility unless your data was way too big for StaticVector in the
first place.

Once you give people a guarantee that an exception will be thrown, they
can go ahead and take advantage of it like above. I'm not sure that
should be encouraged at all, much less in this case where StaticVector
is an optimization and the above is a terribly inefficient idiom.
Violating the otherwise-precondition is easily and cheaply avoided,
which makes it suitable as a precondition.

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