#include <iostream>
#include <locale>
int main(int argc, char** argv) {
std::wcout.imbue(std::locale("zh_CN.UTF-8"));
std::wcout << wchar_t(0) << L"哈哈" << std::endl;
std::cout << char(0) << "haha" << std::endl;
std::cout << "---------------" << std::endl;
std::wcout.clear();
std::wcout << L"哈哈" << std::endl;
std::cout << "haha" << std::endl;
std::cout << "---------------" << std::endl;
std::wcout << L'\0' << L"哈哈" << std::endl;
std::cout << '\0' << "haha" << std::endl;
std::cout << "---------------" << std::endl;
std::wcout.clear();
std::wcout << L"哈哈" << std::endl;
std::cout << "haha" << std::endl;
return 0;
}
The wchar_t(0) and L'\0' seem to different from char(0) and '\0' and cause the ostream to have bad state.
It took me some time to figure out the missing output is not caused by the locale setting but the wchar_t since my original program has somewhere output a wchar_t(0) or '\0'.
My question is how are they different from the char version? And how to correctly use an empty wchar_t?
Thanks in advance.