In C++11 basic_string::c_str is defined to be exactly the same as basic_string::data, which is in turn defined to be exactly the same as *(begin() + n) and *(&*begin() + n) (when 0 <= n < size()).
I cannot find anything that requires the string to always have a null character at its end.
Does this mean that c_str() is no longer guaranteed to produce a null-terminated string?
Strings are now required to use null-terminated buffers internally. Look at the definition of
operator[](21.4.5):Looking back at
c_str(21.4.7.1/1), we see that it is defined in terms ofoperator[]:And both
c_stranddataare required to be O(1), so the implementation is effectively forced to use null-terminated buffers.Additionally, as David Rodríguez – dribeas points out in the comments, the return value requirement also means that you can use
&operator[](0)as a synonym forc_str(), so the terminating null character must lie in the same buffer (since*(p + size())must be equal tocharT()); this also means that even if the terminator is initialised lazily, it’s not possible to observe the buffer in the intermediate state.