std::codecvt_utf8 facet erroneous output with MSYS2 Bash and CMD

208 views Asked by At

Reproduce: codecvt.cpp

#include <iostream>
#include <locale>
#include <string>
#include <codecvt>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);

    locale utf8(locale(), new codecvt_utf8<wchar_t>);
    wcout.imbue(utf8);

    wstring str(L"Test String OÖ UÜ SŞ iİ ıI");
    wcout << str << '\n';
}

Compile with GCC 6.3.0: g++ codecvt.cpp -o codecvt -O3

Windows CMD: codecvt.exe

Output: Test String O├û U├£ S┼Ş i─░ ─▒I

MSYS2 Bash: $ ./codecvt.exe

Output: Test String O├û U├£ S┼Ş i─░ ─▒I (same)

Ubuntu subsystem for Windows 10 Bash:

Compile with GCC 5.4.0: g++ codecvt.cpp -o codecvt -O3

Execute: ./codecvt

Output: Test String OÖ UÜ SŞ iİ ıI

I've been struggling with wide character problems in c++ lately. I ran into this solution but code only seems to be working in linux environment. (I am a c++ beginner (ish))

I also tried some other solutions:

First solution:

setlocale(LC_ALL, "");

Windows CMD outputs as desired, no problems. But input is erroneous.

MSYS2 Bash does not outputs as desired, but input is correct.

Ubuntu subsystem for Windows 10 Bash is correct with both input and output.

Second solution:

locale::global(locale("C.UTF-8"));

Both Windows CMD and MSYS2 Bash crashes at start of program, MSYS2 Bash also reported:

what(): locale::facet::_S_create_c_locale name not valid

Ubuntu subsystem Bash is still cool, works for input/output correcly.

I am guessing this is a problem related to Windows?

0

There are 0 answers