鉴于以下代码段无法编译:
std::stringstream ss; ss << std::wstring(L"abc");
我也不认为这个会:
std::stringstream ss; ss << L"abc";
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
但它确实(至少在 VC++ 上)。我猜这是由于以下ostream::operator<<过载:
ostream::operator<<
ostream& operator<< (const void* val );
如果我无意中混合了字符类型,这是否有可能悄悄地破坏我的代码?
一句话:是的,而且我知道没有解决方法。您只会看到指针值的表示,而不是字符串,因此这不是潜在的崩溃或未定义的行为,只是输出不是您想要的。
是的 - 你需要wstringstream输出wchar_t。
wstringstream
wchar_t
您可以通过不使用字符串文字来缓解这种情况。如您所述,如果您尝试传递const wstring&给stringstream它,它将无法编译。
const wstring&
stringstream