they figured accessing the '\0' after the string is usually the result of an off-by-one error and rarely intentional. If so, they're right. |
std::string
with operator[]
(but not with at()
) one past the end has always been perfectly legal* C++ as long as the access is read-only.If so, they are wrong: accessing a std::string with operator[] (but not with at()) one past the end has always been perfectly legal* C++ as long as the access is read-only. |
[10] is way, way out of bounds on something that has only six elements. With an array of size 6 (let's call it str), the following elements are in bounds: str[0] str[1] str[2] str[3] str[4] str[5] The following are out of bounds: str[6] str[7] str[8] str[9] str[10] str[11] str[12] str[13] and so on, all the way up to str[infinity] The following are also out of bounds: str[-1] str[-2] str[-3] str[-4] str[-5] str[-6] and so on, all the way up to str[-infinity] |
If so, they are wrong: accessing a std::string with operator[] (but not with at()) one past the end has always been perfectly legal* C++ as long as the access is read-only. |
Returns: If pos < size(), returns data()[pos]. Otherwise, if pos == size(), the const version returns charT(). Otherwise, the behavior is undefined. |
|
|