The documentation for the CStringT Class contains the following cryptographic instruction:
Although it is possible to create instances of CStringT containing embedded null characters, we recommend against it. Call methods and statements on CStringT objects that contain embedded null characters can produce unintended results.
Honestly, I really don't know what to do with the final offer. I take this as a warning to be careful when nulling characters. Regardless, contractual warranties must still be respected.
Analysis:
This does not seem to apply to CStringT :: operator + = . In the question code example, the implementation of operator+= calls
CSimpleStringT& operator+=( const CSimpleStringT& strSrc )
overload, which changes the current instance, causing
void Append( const CSimpleStringT& strSrc )
which in turn causes
void Append( PCXSTR pszSrc, int nLength )
passes an argument with an explicit length. This is enough to work with C-style strings with embedded null characters. Oddly enough, the implementation then begins to guess the input by calling StringLengthN(pszSrc, nLength) (implemented as a wcsnlen call) to recalculate the length of pszSrc. This returns 0 for the instance CStringT instance x in the sample code.
Result:
It seems to me that this is a mistake in implementation. By the way, if you cancel the arguments with operator+= (i.e. x += r; vs. r += x; ), the result will be a string with a length of 2, as expected.
Resolution:
The only clean solution is to get Microsoft to acknowledge the error and provide a fix. However, I would not hold my breath, because Microsoft usually does not send bug fixes if they change the behavior of the shipped product.
If you cannot convince Microsoft to fix this error, your only option is to not use an operator with unwanted behavior. One way is to use another class of strings. A well-established replacement for std :: wstring , which you can convert to CStringW if necessary ( CStringW cstr(s.c_str(), s.length()); ).
<h / "> Update (2016-09-13):
The OP filed with Microsoft with Microsoft, and they confirmed the error. A bug fix has been fixed, but "it will not be displayed until the next major version of libraries (which may not necessarily be sent in the current [2016-03-31] Visual Studio vNext)."