Given that the following snippet doesn't compile:
std::stringstream ss;
ss << std::wstring(L"abc");
I didn't think this one would, either:
std::stringstream ss;
ss << L"abc";
But it does (on VC++ at least). I'm guessing this is due to the following ostream::operator<<
overload:
ostream& operator<< (const void* val );
Does this have the potential to silently break my code, if I inadvertently mix character types?
The stringstream class in C++ allows a string object to be treated as a stream. It is used to operate on strings. By treating the strings as streams we can perform extraction and insertion operation from/to string just like cin and cout streams.
stringstream is constructed with dummy. This copies the entire string's contents into an internal buffer, which is preallocated.
Absolutely! Make sure that you pass it by reference, not by value.
The classes in <strstream> are deprecated. Consider using the classes in <sstream> instead.
Yes - you need wstringstream
for wchar_t
output.
You can mitigate this by not using string literals. If you try to pass const wstring&
to stringstream
it won't compile, as you noted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With