Discussion
It is known that from C++11 and beyond
std::basic_strings 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_stringallowed only very limited concurrent access to strings and thus, limited performance for multi-threaded applications. (More on the reasons for the changes instd::basic_stringcan be read in the proposal N2534).However, reading the standard I couldn't find a quote where explicitly is stated that
std::basic_stringmust 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;
1Returns: A pointerpsuch thatp + i == &operator[](i)for eachiin[0,size()].3Requires: 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/3require 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_stringand not some null character terminated copy.
Questions:
- Is there somewhere in the standard explicitly stated that
std::basic_stringinternal 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_stringwith out a null character terminated internal storage buffer and consequently the wide spread notion that since C++11 strings are null terminated is wrong?