microsoft / STL

MSVC's implementation of the C++ Standard Library.
Other
9.88k stars 1.45k forks source link

Should `std::error_code::message` respect the locale set by the user? #4711

Open solarispika opened 3 weeks ago

solarispika commented 3 weeks ago

In our program, we use std::setlocale(LC_ALL, ".UTF8"); to support UTF-8 encoding, as noted in the Microsoft documentation. However, when certain C++ STL functions fail, their message() outputs are not in UTF-8.

After investigating the source code, I found that the error messages are always formatted according to the system's default locale, as shown in this STL implementation: https://github.com/microsoft/STL/blob/e36ee6c2b9bc6f5b1f70776c18cf5d3a93a69798/stl/src/syserror_import_lib.cpp#L38-L54

While it would be acceptable if the system locale used UTF-8 as the codepage, allowing the messages to be correctly printed to log files, this is not the case. There is a "Beta: Use Unicode UTF-8 for worldwide language support" setting in the Region settings, which does the job correctly, but toggling that setting requires a reboot, which is not acceptable for our requirements.

Question

Should the std::error_code::message function respect the locale set by the user (e.g., via std::setlocale), or should it continue to use the system's default locale? If the former, is there a plan to implement this behavior in the Microsoft STL implementation?

Related Information

Please let me know if you need any additional information or clarification.

jovibor commented 3 weeks ago

This is basically a duplicate of the #3254. Also see the #3260.

miyanyan commented 3 weeks ago

I have the same question, when I get filesystem_error, it's message often uses the system locale, and I have to change the message to utf8

StephanTLavavej commented 2 weeks ago

@jovibor Yep, agreed. Marking as "bug" but not resolving as a duplicate, until we review the pending PR.