[Effective-cpp] Item 1: Uses and Abuses of vector

Ric Parkin ric.parkin at ntlworld.com
Tue Oct 26 09:29:06 EDT 2004


From: "Balog Pal" <pasa at lib.hu>
> > As for the question that came up about when would you use at? Well, it's
> where you want to access an element by index, you have to validate the
> index, and you want an invalid index signalled by throwing an out_of_range
> exception.
> 
> Sure, the question was about 'when you have such a situation IRL'. :)
> 
> I hope the 'validate the index' does not mean inserting manual checks, but
> includes the cases where the local environment makes sure it is valid, or
> the design, preconditions, etc guarantee the validness.

No, I mean that if the design, preconditions, etc shows it can't be out of range, then checks are *not* needed - the check has either been done, cannot happen, or is a programmer error that needs to fixed - so using at is pointless.

You use at if, and only if: the index might be invalid; no one else has done checks; you have to do the checks at the same point as the access; and you want failure indicated by out_of_range.
 
Ric


-----------------------------------------
Email provided by http://www.ntlhome.com/





More information about the Effective-cpp mailing list