Mingw, wcout and locales I've recently had to print some national symbols in windows console using Mingw and found, that I got nothing in the output, if I use wide strings.

So, I studied the problem and found out that it is locale problem. I was unable to find somewhere complete solution, so I had to write something, that seems to work. 

So, I decided to publish it here for review, or may be I should publish it somewhere it it worth.


I've tested in Mingw 4.7.0 using Russian locale.
It should probably correctly convert wide strings to national OEM encoded strings.


Code:

#ifndef CUSTOM_LOCALE_H
#define CUSTOM_LOCALE_H

#if (__cplusplus &gt;= 201103L)
#define CUSTOM_LOCALE_OVERRIDE override
#else 
#define CUSTOM_LOCALE_OVERRIDE
#endif

#include &lt;locale&gt;
#include &lt;iostream&gt;
#include &lt;windows.h&gt;

namespace custom_locale
{

class CustomLocale : public std::codecvt &lt;wchar_t, char, std::mbstate_t&gt; {
  public:
    explicit CustomLocale ( size_t r = 0 ) : std::codecvt &lt;wchar_t, char, std::mbstate_t&gt; (r) {}

  protected:
    result do_in (state_type&amp;, const char* from, const char* from_end, 
                  const char*&amp; from_next, wchar_t* to, wchar_t* to_end, wchar_t*&amp; to_next ) const CUSTOM_LOCALE_OVERRIDE
    {
        std::size_t size = from_end - from;
        std::size_t buffer_size = to_end - to;
        std::size_t written = MultiByteToWideChar(CP_OEMCP, 0, from, size, to, buffer_size);
        to_next = to + written;
        if (written == 0) {
            return error;
        }
        else if (written != buffer_size) {
            return partial;
        }
        else {
            return  ok;
        }
    }

    result do_out (state_type&amp;, const wchar_t* from, const wchar_t* from_end, 
                   const wchar_t*&amp; from_next, char* to, char* to_end, char*&amp; to_next ) const CUSTOM_LOCALE_OVERRIDE
    {
        std::size_t size = from_end - from;
        std::size_t buffer_size = to_end - to;
        std::size_t written = WideCharToMultiByte(CP_OEMCP, 0, from, size, to, buffer_size, 0, 0);
        to_next = to + written;
        if (written == 0) {
            return error;
        }
        else if (written != buffer_size) {
            return partial;
        }
        else {
            return  ok;
        }
    }

    result do_unshift ( state_type&amp;, char*, char*, char*&amp; ) const CUSTOM_LOCALE_OVERRIDE { return ok; }
    int do_encoding () const throw () CUSTOM_LOCALE_OVERRIDE { return 1; }
    bool do_always_noconv () const throw () CUSTOM_LOCALE_OVERRIDE { return false; }

    int do_length ( state_type&amp; state, const char* from, const char* from_end, size_t max ) const CUSTOM_LOCALE_OVERRIDE 
    {
      return std::codecvt &lt;wchar_t, char, std::mbstate_t&gt;::do_length ( state, from, from_end, max );
    }

    int do_max_length () const throw () CUSTOM_LOCALE_OVERRIDE
    {
      return std::codecvt &lt;wchar_t, char, std::mbstate_t&gt;::do_max_length ();
    }
};

inline void init()
{
    std::locale loc ( std::locale(), new CustomLocale() );
    std::ios_base::sync_with_stdio (false);
    std::wcout.imbue(loc);
    std::wcin.imbue(loc);
} 

}

#endif


Example:

#include "custom_locale.h"

int main ()
{
    custom_locale::init();    
    std::wstring s(L"привет");
    std::wcout &lt;&lt; s &lt;&lt; '\n';
    std::wstring u; 
    std::wcin &gt;&gt; u;
    s += L' ' + u;
    std::wcout &lt;&lt; s &lt;&lt; '\n';
    return 0;
}
This is a somewhat old question, but I have an alternative that may work for this.  Related to this question, if you turn off the synchronization with the underlying stdio, you can use imbue to set the locale for both wcout and wcin:

#include &lt;locale&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;

int main ()
{
    // this code turns off the sync with stdio
    std::ios_base::sync_with_stdio(false);
    // now create the appropriate locale without setting the global one
    std::locale ru("ru_RU.utf8");
    // use imbue to allow the Russian words to be properly displayed
    std::wcout.imbue(ru);
    // ... and input
    std::wcin.imbue(ru);

    // the rest of the code is essentially unchanged from the original
    std::wstring s(L"привет");
    std::wcout &lt;&lt; s &lt;&lt; '\n';
    std::wstring u; 
    std::wcin &gt;&gt; u;
    s += L' ' + u;
    std::wcout &lt;&lt; s &lt;&lt; '\n';
 }


Using it in this way means that no custom class is required.