Rather than outputting the expected string of "Bar", the following code outputs what looks to be a pointer.
#include <sstream>
#include <iostream>
int main()
{
std::stringstream Foo("Bar");
std::cout << Foo << std::endl; // Outputs "0x22fe80."
return 0;
}
This can be worked around by using Foo.str(), but it’s caused some issues for me in the past. What causes this strange behavior? Where is it documented?
It’s not strange at all.
A stream is a stream, not a string.
You can obtain the [string] buffer with the member function
.str(). That’s not a workaround: it’s the API. It should be documented in your C++ standard library reference, and if it’s not then your reference is incomplete/wrong.(What is slightly strange is that you get a memory address rather than a compilation error; that is due to the use of the safe bool idiom in the pre-C++0x standard library, as covered in another answer.)