The “story” was that, a long time ago, when everyone was working in single threads, or at least the threads were working with their own data, they developed a string class for C ++ that made it easier to process strings than before, and they overloaded the + operator to concatenate strings.
The problem was that users would do something like:
s = s1 + s2 + s3 + s4;
and each concatenation would create a temporary value that the string should execute.
Therefore, someone had a brain wave of "lazy evaluation", so that inside you could store some kind of "rope" with all the lines, until someone wanted to read it as a C-line, at that moment you would change the internal representation to a continuous buffer.
This solved the problem above, but caused a load of other headaches, in particular in a multi-threaded world where it was expected that the .c_str () operation would be read-only / does not change anything and, therefore, there is no need to block anything. Premature internal locking in the class implementation just in case when someone was doing this multithreaded (when there was not even a thread standard) was also not very good. It was actually more expensive than just copying the buffer every time. For the same reason, the implementation of "copy on write" was left to implement strings.
Thus, creating .c_str() truly immutable operation turned out to be the most reasonable task, however, can you rely on it in the standard that is now known by the stream? Therefore, the new standard decided to clearly indicate what you can, and thus, the internal representation needs to be kept null terminator.
CashCow Oct 24 '12 at 11:15 2012-10-24 11:15
source share