Skip to content

Reconsider the implemention of std::print #6484

Closed

Description

My understanding of the current situation is as follows:

  1. Each console program is associated with a conhost process, and that process holds the console state.
  2. The state of ConsoleOutputCP is stored in the conhost process, and by default it is conhost's ACP OEMCP mostly.
  3. The UTF-8 switch in Windows settings changes conhost's ACP OEMCP to UTF-8.
  4. SetConsoleOutputCP changes the state of conhost.
  5. When WriteFile/WriteConsoleA is used to output to the console, conhost interprets the encoding used by the bytes according to the value of ConsoleOutputCP.
  6. Correct output is possible only when the encoding of the bytes written by WriteFile/WriteConsoleA matches the value of SetConsoleOutputCP.
  7. conhost always interprets the bytes written by WriteConsoleW as UTF-16.
  8. The encoding of literals in a C++ program depends on the compiler options.
  9. The program's ACP determines the encoding used by some strings in the standard library, such as exception strings and filesystem::path::string.
  10. The encoding used by literals, the ACP, the OEMCP, and ConsoleOutputCP are independent of one another.
  11. There is no efficient API to determine whether a handle is a console; GetFileType reports FILE_TYPE_CHAR for both NUL and consoles.
  12. NtQueryVolumeInformationFile provides 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_unicode and 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 is WriteConsoleW and stream referring to a terminal means that GetConsoleMode(_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

  1. added
    question
    on Oct 9, 2026
  2. DHowett commented on Oct 9, 2026

    @DHowett
    Member

    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!)

  3. lhecker commented on Oct 9, 2026

    @lhecker
    Member

    Your analysis is larger correct, with some subtle omissions. (For instance, the initial value of ConsoleOutputCP is not always the ACP.) If you have any specific questions or areas of interest I'm happy to talk about it.

    NtQueryVolumeInformationFile provides the capability to accurately determine whether a handle is a console, but this function is not exported to any lib

    It is exported, just not by the Win32 SDK. It's part of the WDK and is a stable API.

    and the constants used by the console are not listed in the documentation learn.microsoft.com/en-us/windows-hardware/drivers/kernel/specifying-device-types, the STL cannot use it until stability is guaranteed.

    The constant is in the list! It's FILE_DEVICE_CONSOLE.

  4. YexuanXiao commented on Oct 9, 2026

    @YexuanXiao
    ContributorAuthor

    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)

  5. DHowett commented on Oct 9, 2026

    @DHowett
    Member

    FWIW, WriteConsoleW with 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.

  6. lhecker commented on Oct 9, 2026

    @lhecker
    Member

    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() with CP_UTF8. But on Windows anyone can call SetConsoleOutputCP at any time. And the STL cannot know that this happened. This makes it impossible to know when writing UTF8 with WriteFile() is safe. You would need to call GetConsoleOutputCP every 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 GetConsoleOutputCP value client-side. But that requires changes to Windows that are quite significant (including new ABIs).

  7. YexuanXiao commented on Oct 9, 2026

    @YexuanXiao
    ContributorAuthor

    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::print to pass UTF-8 bytes directly to conhost.

  8. lhecker commented on Oct 9, 2026

    @lhecker
    Member

    Yexuan Xiao (@YexuanXiao) You probably wrote your response before you read mine, right? As I mentioned, you would need to call GetConsoleOutputCP every 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).

  9. YexuanXiao commented on Oct 9, 2026

    @YexuanXiao
    ContributorAuthor

    I now have all the information I wanted. Thank you both for your patient and professional replies. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions