In case of std::string, if we access an element where
(element position) == (size of string) the standard says that it returns a reference to an object of type
charT with value
const_reference operator(size_type pos) const; reference operator(size_type pos);
Expects: pos <= size().
Returns: *(begin() + pos) if pos < size(). Otherwise, returns a reference to an object of type charT with value charT(), where modifying the object to any value other than charT() leads to undefined behavior.
Unfortunately I couldn't reason about this, it would have been better if it has been Undefined Behavior.
Can somebody explain the rationale behind this?