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

Balog Pal pasa at lib.hu
Tue Oct 26 09:47:20 EDT 2004


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

> This last point tends to be missed as far I have seen: if you want some
other way of signalling it, eg a magic value, or a domain layer specific
exception, then it's often easier and clearer to test by hand, and then call
[] if everything's okay.
>
> And sometimes, I'll just define an interface's contract that passing in
out of range indexes results in undefined behaviour. Then I've a choice of
whether to catch errors and how to report them.
>
> So I don't tend to use at very often at all.

Exactly my view. :)




More information about the Effective-cpp mailing list