#include <iostream>
using namespace std;
void f1()
{
wcout.imbue(locale("chs"));
wcout << L"您" << endl;
}
void f2()
{
locale loc(wcout.getloc(), new codecvt<wchar_t, char, mbstate_t>());
wcout.imbue(loc);
wcout << L"好" << endl;
}
int main()
{
f1(); // OK
f2(); // Error. There is no output as expected.
}
According to cplusplus.com's online documentation:
codecvt<wchar_t,char,mbstate_t>:
converts between native wide and narrow character sets.
This program is compiled with VC++, and runs on Windows.
In this program, the internal character set is UCS-2, which is defined by the VC++ compiler; the external character set, i.e. the narrow character set, is GBK (a Chinese character set) in console environment. If the documentation is true, then wcout
can convert the unicode string from UCS-2 to GBK as f1()
does; however, it does not. Why?