This is correct, and by design.
All streams behave this way. Life would be very frustrating if it didn't.
For example:
1 2 3 4 5
|
int main()
{
std::cout << "Hello ";
std::cout << "world!";
}
|
What do you expect to see on the screen?
The text on the left is correct. If all we got was the text on the right, life would suck.
A string stream is no different, in fact, it satisfies its purpose: reuse C++ stream capabilities to compose a string from smaller parts:
"Hello " + name + "! I hear you are " + age + " years old!"
That should immediately look kind of familiar. C++ phrases it like this:
cout << "Hello " << name << "! I hear you are " << age << " years old!";
That
cout can be replaced with
any valid output stream. Like a stringstream.
1 2 3
|
ostringstream ss;
ss << "Hello " << name << "! I hear you are " << age << " years old!";
string s = ss.str();
|
Don't be confundled by the fact that not all output has to happen in the same statement. You can split it up in multiple lines, as always. The behavior is consistent.
1 2 3 4 5
|
ostringstream ss;
ss << "Hello ";
// do something else here, like call a function or whatever //
ss << "world!";
string s = ss.str();
|
Being consistent, we can rightly expect
s to have the value
"Hello world!".
Hope this helps.