문제

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?

도움이 되었습니까?

해결책

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

In a word: yes, and there is no workaround that I know of. You'll just see a representation of a pointer value instead of a string of characters, so it's not a potential crash or undefined behaviour, just output that isn't what you want.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top