Discussion
It is known that from C++11 and beyond
std::basic_string
s are considered to have null character terminated internal storage buffers.The main reason for this change, among others, was that the previous definition of
std::basic_string
allowed only very limited concurrent access to strings and thus, limited performance for multi-threaded applications. (More on the reasons for the changes instd::basic_string
can be read in the proposal N2534).However, reading the standard I couldn't find a quote where explicitly is stated that
std::basic_string
must have a null character terminated internal storage buffer.The only implicit quote that I've found is §21.4.7.1/1&3 basic_string accessors [string.accessors]:
const charT* c_str() const noexcept;
const charT* data() const noexcept;
1
Returns: A pointerp
such thatp + i == &operator[](i)
for eachi
in[0,size()]
.3
Requires: The program shall not alter any of the values stored in the character array.
I assume that due to efficiency reasons and since
§21.4.7.1/3
require that the program shall not alter the returned buffer, most implementers instd::basic_string::c_str()
andstd::basic_string::data()
are returning the null character terminated internal buffer.However, the standard doesn't state anywhere that the buffer that must be returned by
std::basic_string::c_str()
andstd::basic_string::data()
must be the internal storage buffer of thestd::basic_string
and not some null character terminated copy.
Questions:
- Is there somewhere in the standard explicitly stated that
std::basic_string
internal storage buffer must be null character terminated? - In case there is not an explicit statement (i.e., question #1 short answer is no), does this mean that an implementer could implement the
std::basic_string
with out a null character terminated internal storage buffer and consequently the wide spread notion that since C++11 strings are null terminated is wrong?