We do not know. This is completely up to implementation. (At least until C ++ 03 - apparently C ++ 11 requires the internal buffer to end with 0.) You can look at the source code for the implementation of the standard C ++ library if the one you are using is open source.
In addition, I would consider it logical if it was completed with NUL and , and it retained an explicit length. This is good, because then it takes constant time to return the length and valid string of C:
size_t length() { return m_length; } const wchar_t *c_str() { return m_cstr; }
If it did not preserve the explicit length, then size() would have to count the characters before NUL in O(n) , which is wasteful if you can avoid it.
If, however, the internal buffer was not NUL-terminated, but it only kept the length, then it would be tedious to create a valid C line with zero completion: the line should either redistribute its storage or add 0 (and redistributing is an expensive operation), or will have to copy the entire buffer, which again is O(n) operation.
(Warning: shameless self-promotion - in a C project that I am currently working on, I used this approach to implement flexible string objects.)
user529758
source share