Strings are now required to use null-terminated buffers internally. Look at the definition of operator[]
(21.4.5):
Requires:
pos <= size()
.Returns:
*(begin() + pos)
ifpos <
, otherwise a reference to an object of type
size()T
with value
charT()
; the referenced value shall not be modified.
Looking back at c_str
(21.4.7.1/1), we see that it is defined in terms of operator[]
:
Returns: A pointer
p
such thatp + i == &operator[](i)
for eachi
in[0,size()]
.
And both c_str
and data
are 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 for c_str()
, so the terminating null character must lie in the same buffer (since *(p + size())
must be equal to charT()
); this also means that even if the terminator is initialised lazily, it’s not possible to observe the buffer in the intermediate state.