wcout not writing wide character out to command prompt

蹲街弑〆低调 提交于 2019-12-23 04:24:26

问题


I am attempting to write the following character out in windows command prompt: ュ (U+FF6D).

I am able to see the character get written out using WriteConsoleW. I am also able to see the character if i use WideCharToMultiByte using the CP_ACP code page (chcp returns 932: Japanese). However, when I attempt to use regular wcout on the same string which WriteConsoleW successfully prints, it chokes.

When I execute setlocale(LC_ALL, "") it prints English_UnitedStates.1252 (the default code page that I had when I installed).

Why is wcout failing when the others are succeeding?

Note: I rebooted the machine to change its system locale to Japan Japanese


回答1:


The default locale for C++ iostreams is always the "C" locale. From the C++03 standard, §27.4.2.3/4:

locale getloc() const;

If no locale has been imbued, a copy of the global C++ locale, locale(), in effect at the time of construction.

From §22.1.1.2/1-2:

locale() throw();

Default constructor: a snapshot of the current global locale.

Constructs a copy of the argument last passed to locale::global(locale&), if it has been called; else, the resulting facets have virtual function semantics identical to those of locale::classic().

From §22.1.1.5/4-6:

static const locale& classic();

The "C" locale.

Returns: A locale that implements the classic "C" locale semantics, equivalent to the value locale("C").

Notes: This locale, its facets, and their member functions, do not change with time.

As std::cout and std::wcout have static storage duration, they are guaranteed to be initialized before main is called, and consequently will always have the "C" locale at application startup; i.e., there is no point early enough in execution that one can call locale::global and change the default locale for std::cout and std::wcout. Thus, you must always imbue the global streams yourself if you want to use a non-default code page.




回答2:


wcout is created before any code in main executes. By the time you call setlocale, wcout is already there, ready to do its thing. It makes no attempt at tracking subsequent changes you might make with setlocale, so it continues to use the default instead of whatever you set with setlocale.



来源:https://stackoverflow.com/questions/5750884/wcout-not-writing-wide-character-out-to-command-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!