Is there any explanation in the standard for the below behavior?
The following code:
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
ostringstream os1;
ostringstream os2;
os1 << 1 << " " << 2;
os2 << 1 << " " << 2 << " " << 3;
const char *p = os1.str().c_str();
cout << os2.str() << endl;
cout << p << endl;
return 0;
}
displays the output:
1 2 3
1 2 3
However, I would expect it to display:
1 2 3
1 2
It looks like os1 object is somehow influenced by os2, if I remove the os2.str() call, example behaves correctly.
I have tried the example if Solaris Studio 12.2 and G++ 4.8.1 and both behave in the same way.
Thanks for your help!