How does std :: stringstream handle wchar_t * in the << operator?

Given that the following snippet does not compile:

 std::stringstream ss; ss << std::wstring(L"abc"); 

I also did not think it would be:

 std::stringstream ss; ss << L"abc"; 

But this happens (at least in VC ++). I assume this is due to the following ostream::operator<< overload:

 ostream& operator<< (const void* val ); 

Does this have the potential to quietly break my code if I inadvertently mix character types?

+4
source share
2 answers

Does this have the potential to quietly break my code if I inadvertently mix character types?

In a word: yes, and there is no workaround that I know of. You will simply see a representation of the pointer value instead of a character string, so this is not a potential glitch or undefined behavior, just an output that you do not need.

+3
source

Yes - you need wstringstream to output wchar_t .

You can reduce this without using string literals. If you try to pass const wstring& to a stringstream , it will not compile as you noted.

+8
source

All Articles