Hi CornedBee,
Here is the current implementation of STL vector from Visual Studio 2008. Looks like for operator[], both release and debug version checks for boundary. So how about your points of performance improvements and speed? Any comments?
Code:
#if !defined (_HAS_ITERATOR_DEBUGGING)
#if defined (_DEBUG)
#define _HAS_ITERATOR_DEBUGGING 1 /* for range checks, etc. */
#else
#define _HAS_ITERATOR_DEBUGGING 0
#endif /* defined (_DEBUG) */
#else
reference operator[](size_type _Pos)
{ // subscript mutable sequence
#if _HAS_ITERATOR_DEBUGGING
if (size() <= _Pos)
{
_DEBUG_ERROR("vector subscript out of range");
_SCL_SECURE_OUT_OF_RANGE;
}
#endif /* _HAS_ITERATOR_DEBUGGING */
_SCL_SECURE_VALIDATE_RANGE(_Pos < size());
return (*(_Myfirst + _Pos));
}
reference at(size_type _Pos)
{ // subscript mutable sequence with checking
if (size() <= _Pos)
_Xran();
return (*(begin() + _Pos));
}
static void _Xran()
{ // report an out_of_range error
_THROW(out_of_range, "invalid vector<T> subscript");
}
Originally Posted by
CornedBee
It's consistent with C syntax, and the expectation is that all implementations, in release mode, are also consistent with C, i.e. they simply allow the out-of-bounds access.
regards,
George