Hi, Alf, Thank you, you made me remember that the author occultly uses a header:
http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
So I decided to investigate if he was doing a special vector version, as you said.
See this code section:
template< class T> struct Vector : public std::vector<T> {
using size_type = typename std::vector<T>::size_type;
#ifdef _MSC_VER
// microsoft doesn't yet support C++11 inheriting constructors
Vector() { }
explicit Vector(size_type n) :std::vector<T>(n) {}
Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
template <class I>
Vector(I first, I last) : std::vector<T>(first, last) {}
Vector(initializer_list<T> list) : std::vector<T>(list) {}
#else
using std::vector<T>::vector; // inheriting constructor
#endif
T& operator[](unsigned int i) // rather than return at(i);
{
if (i<0||this->size()<=i) throw Range_error(i);
return std::vector<T>::operator[](i);
}
const T& operator[](unsigned int i) const
{
if (i<0||this->size()<=i) throw Range_error(i);
return std::vector<T>::operator[](i);
}
};
More specifically this part:
T& operator[](unsigned int i) // rather than return at(i);
{
if (i<0||this->size()<=i) throw Range_error(i);
return std::vector<T>::operator[](i);
}
I'm still learning c ++, but I can see by this code that he creates its own vector version, through inheritance, specializing the operator[].
Conclusions:
1-The standard vector in STL doesn't throw out_of_range when out of range indexing.
2-Using standard vector STL, If you want this characteristic you have to use .at(index)
3-The examples in book PPP2 use a modified vector which throws out of range indexing.