In practice, all STL implementations that I've looked at (VC, GCC, etc.) do exactly what you want. They might have some bounds checking in debug/development builds, but they omit them all when you are building for release. This is true (AFAIK) for Boost containers too (e.g. see here, as mentioned in another answer.)
In some cases, different access methods have different behaviors in terms of bounds checking. For example, the std::vector
member function at()
always does perform bounds checks, but the indexing operator ([]
) doesn't do so in release/nondebug builds.
In the end, if it is this important to you, you should just look at the implementation you are using and make sure. It's just a handful of functions on a few containers.
About the performance cost of bounds checking. In practice, all the provided STL algorithms will use unchecked access into the containers (they do their error checking up front and from then on won't bother.) Therefore, if you use STL algorithms, you won't see any performance overhead whatsoever.
In your own code, inside your tight inner loops, you might see a negative impact on performance from bound checks. I haven't done any benchmarks but I imagine it would be minimal and negligible in most cases. Also, in the vast majority of use cases, you can replace your hand-written loops with STL algorithms (for_each
, accumulate
, count_if
, etc.) which will eliminate even that minimal cost.