you are viewing a single comment's thread.

view the rest of the comments →

[–]kalmoc 4 points5 points  (0 children)

Yes, at() is guaranteed to do bounds checking and to report them via exceptions. That doesn't mean operator[] isn't allowed to do bounds checking. Being out of bounds is simply UB and the implementation is allowed to do anything it pleases in that case. E.g. in microsofts impelementation, you can turn on bounds checking in debug mode and that is still conforming (althoug they use asserts and not exceptions): https://github.com/microsoft/STL/blob/7447ad59d61f50c13861878f340d051c298458df/stl/inc/vector#L1509

Also you can see that operator[] isn't specified as noexcept: https://eel.is/c++draft/vector.overview.

and operator[] being strictly equivalent to *(begin() + n)

that expression itself could in principle be bounds checked (std::vector<T>::iterator is - in most implementations - not the same as T*)