[opensource-dev] Replacement class for LLDynamicArray

Henri Beauchamp sldev at hotmail.com
Fri May 9 08:12:07 PDT 2014


On Fri, 9 May 2014 15:37:15 +0200, Nicky D. wrote:

> > > I notice "operator[](i)" is used in the interesting,
> >
> > With std::vector, you could also use array.at(i), which is equivalent.
>
> vector::at will do a runtime check if the index is out of bounds, in
> that case it throws an exception.
> 
> vector::operator[] will not do this check, causing undefined behavior
> when accessing elements out of bounds.

True, but on the other hand, you'd never call array[i] with i out of
array bound (it would be a bug, and throwing an exception via the use
of at(i) is no better than "undefined behaviour" that will also lead
to a crash in the end). The fact that array[i] doesn't check the upper
bound also makes it faster than array.at(i): competent programmers who
do check for bounds where actually needed will therefore prefer
array[i] to array.at(i), esspecially when used in a loop !

Henri.


More information about the opensource-dev mailing list