According to Herb Sutter one marked reason is efficiency. He states that the standard does not impose any requirements on operator[]
's exception specification or whether or not it requires bound checking. This is up to the implementation.
On the other hand, vector<T>::operator[]()
is allowed, but not
required, to perform bounds checking. There's not a breath of wording
in the standard's specification for operator[]()
that says anything
about bounds checking, but neither is there any requirement that it
have an exception specification, so your standard library implementer
is free to add bounds-checking to operator[]()
, too. So, if you use
operator[]()
to ask for an element that's not in the vector, you're
on your own, and the standard makes no guarantees about what will
happen (although your standard library implementation's documentation
might) -- your program may crash immediately, the call to
operator[]()
might throw an exception, or things may seem to work
and occasionally and/or mysteriously fail.
Given that bounds checking protects us against many common problems,
why isn't operator[]()
required to perform bounds checking? The
short answer is: Efficiency. Always checking bounds would cause a
(possibly slight) performance overhead on all programs, even ones that
never violate bounds. The spirit of C++ includes the dictum that, by
and large, you shouldn't have to pay for what you don't use, and so
bounds checking isn't required for operator[]()
. In this case we
have an additional reason to want the efficiency: vectors are intended
to be used instead of built-in arrays, and so should be as efficient
as built-in arrays, which don't do bounds-checking. If you want to be
sure that bounds get checked, use at()
instead.
If you're curious about the performance benefits, see these two questions:
- ::std::vector::at() vs operator[] << surprising results!! 5 to 10 times slower/faster!
- vector::at vs. vector::operator[]
The consensus seems to be that operator[]
is more efficient (since std::vector
is just a wrapper around a dynamic array, operator[]
should be just as efficient as if you would call it on an array.) And Herb Sutter seems to suggest that whether or not it is exception-safe is up to the compiler-vendor.