Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2019-11-27 17:23:07


Zach Laine wrote:
> On Wed, Nov 27, 2019 at 11:01 AM Peter Dimov via Boost
> <boost_at_[hidden]> wrote:
> > Zach Laine wrote:
> >
> > > Throwing when the capacity would be exceeded is wrong.
> >
> > No, it's not. It's correct. It's a runtime error, not a programming time
> > error.
>
> Then why doesn't vector::operator[] throw? In either case, you know what
> the precondition is, and how to check it. That's what makes this a user
> error.

What makes op[] a user error is that the index rarely comes from external
input. It typically comes from a variable that the programmer controls and
program logic ensures is in range.

Whereas when you += strings into a fixed capacity buffer, those are only
occasionally programmer-supplied. Program logic is not violated if the input
strings exceed the buffer.

Or stated differently, if in 90% of the cases the correct use of a function
would involve the programmer writing out the exact same

    if( !precondition ) throw exception();

thing that the function would have done itself were it throwing, the
function should be throwing.

Whereas when you do `for( int i = 0; i < n; ++i ) { something with v[i]; }`,
you don't need to insert the above before each [].


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