I'm licking my wounds from Memory Error with std:ostringstream and -std=c++11?, and I have a related question.
If the following returns a temporary so that reserve
has no effect and the char*
is not valid:
ostringstream oss;
oss.str().reserve(96);
// populate oss
const char* ptr = oss.str().c_str();
// do something with ptr
Then how does the following clear the ostringstream
(from How to reuse an ostringstream?):
oss.clear(); oss.str("");
I understand clear()
will reset the stream's flags; but as I now understand str("")
will operate on a temporary and not the underlying string.
So how does str("")
reset the stream?