Repository navigation
Description
My understanding of the current situation is as follows:
- Each console program is associated with a conhost process, and that process holds the console state.
- The state of
ConsoleOutputCPis stored in the conhost process, and by default it is conhost'sACPOEMCP mostly. - The UTF-8 switch in Windows settings changes
conhost'sACPOEMCP to UTF-8. SetConsoleOutputCPchanges the state ofconhost.- When
WriteFile/WriteConsoleAis used to output to the console,conhostinterprets the encoding used by the bytes according to the value ofConsoleOutputCP. - Correct output is possible only when the encoding of the bytes written by
WriteFile/WriteConsoleAmatches the value ofSetConsoleOutputCP. - conhost always interprets the bytes written by
WriteConsoleWas UTF-16. - The encoding of literals in a C++ program depends on the compiler options.
- The program's ACP determines the encoding used by some strings in the standard library, such as exception strings and
filesystem::path::string. - The encoding used by literals, the ACP, the OEMCP, and
ConsoleOutputCPare independent of one another. There is no efficient API to determine whether a handle is a console;GetFileTypereportsFILE_TYPE_CHARfor both NUL and consoles.NtQueryVolumeInformationFileprovides the capability to accurately determine whether a handle is a console, but this function is not exported to any lib, and the constants used by the console are not listed in the documentation https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/specifying-device-types, the STL cannot use it until stability is guaranteed.
Regarding std::print, the C++ standard has designed two APIs: one is std::vprint_unicode, and the other is std::vprint_nonunicode. When the literal encoding is UTF-8, std::print calls std::vprint_unicode; otherwise, it calls std::vprint_nonunicode.
The standard also stipulates the following:
If the function is
vprint_unicodeand os is a stream that refers to a terminal that is capable of displaying Unicode only via a native Unicode API, which is determined in an implementation-defined manner, flushes os and then writes out to the terminal using the native Unicode API..., On Windows the native Unicode API isWriteConsoleWand stream referring to a terminal means thatGetConsoleMode(_get_osfhandle(_fileno(stream)), ...)returns nonzero.
I am not sure whether the standard's description of "native Unicode API" is correct, and whether this implementation approach meets the expectations of the Windows console.
Also related to #6483.
Dustin L. Howett (@DHowett) Could you explain the console team's perspective on this issue?
Activity
DHowett commentedon Oct 9, 2026
Leonard Hecker (@lhecker) may be better equipped to answer (not because I do not have opinions, but because he has owned our input and output stack for quite a while now!)
lhecker commentedon Oct 9, 2026
Your analysis is larger correct, with some subtle omissions. (For instance, the initial value of
ConsoleOutputCPis not always the ACP.) If you have any specific questions or areas of interest I'm happy to talk about it.It is exported, just not by the Win32 SDK. It's part of the WDK and is a stable API.
The constant is in the list! It's
FILE_DEVICE_CONSOLE.YexuanXiao commentedon Oct 9, 2026
Currently, when STL outputs UTF-8 strings, it transcodes them to UTF-16 and then uses WriteConsoleW for output. Which function is the recommended output method? Leonard Hecker (@lhecker)
DHowett commentedon Oct 9, 2026
FWIW,
WriteConsoleWwith UTF-16 will work correctly regardless of the ACP, output CP, or any other externalities. This may insulate the STL against breaking if the application changes the ANSI codepage in any way.lhecker commentedon Oct 9, 2026
Yexuan Xiao (@YexuanXiao) For STL, the best approach is unfortunately
WriteConsoleW.In an ideal world in 2026, every C++ app would use UTF8 and you would just call
WriteFile()withCP_UTF8. But on Windows anyone can callSetConsoleOutputCPat any time. And the STL cannot know that this happened. This makes it impossible to know when writing UTF8 withWriteFile()is safe. You would need to callGetConsoleOutputCPevery time but that's a remote RPC function, so it's way too costly to call.I had in mind to change this and to cache the
GetConsoleOutputCPvalue client-side. But that requires changes to Windows that are quite significant (including new ABIs).YexuanXiao commentedon Oct 9, 2026
Windows now supports UTF-8 as ACP. If an application chooses to do so, then the STL is performing unnecessary transcoding (and the STL currently even has bugs at UTF-8 code point boundaries). I want to discuss whether there should be a mechanism to allow
std::printto pass UTF-8 bytes directly to conhost.lhecker commentedon Oct 9, 2026
Yexuan Xiao (@YexuanXiao) You probably wrote your response before you read mine, right? As I mentioned, you would need to call
GetConsoleOutputCPevery time you write to know whether writing UTF8 is safe.However, UTF16 transcoding runs at ~2GB/s via
MultiByteToWideChar, which far exceeds the speed of any PTY (including those on UNIX). And more importantly: Since the STL performs only line buffering (not full buffering) the RPC overhead will dominate the UTF16 transcoding overhead by probably a factor of almost 1000x (genuinely - I just benchmarked it).YexuanXiao commentedon Oct 9, 2026
I now have all the information I wanted. Thank you both for your patient and professional replies. 😁