title: Output unicode strings in Windows console app
body: <p>Hi I was trying to output unicode string to a console with <em>iostreams</em> and failed.</p>
  
  <p>I found this:  <a href="https://stackoverflow.com/questions/1922294/using-unicode-font-in-c-console-app">Using unicode font in c++ console app</a>  and this snippet works.</p>
  
  <pre><code>SetConsoleOutputCP(CP_UTF8);
  wchar_t s[] = L"èéøÞǽлљΣæča";
  int bufferSize = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL);
  char* m = new char[bufferSize]; 
  WideCharToMultiByte(CP_UTF8, 0, s, -1, m, bufferSize, NULL, NULL);
  wprintf(L"%S", m);
  </code></pre>
  
  <p>However, I did not find any way to output unicode correctly with iostreams. Any suggestions?</p>
  
  <p>This does not work:</p>
  
  <pre><code>SetConsoleOutputCP(CP_UTF8);
  utf8_locale = locale(old_locale,new boost::program_options::detail::utf8_codecvt_facet());
  wcout.imbue(utf8_locale);
  wcout &lt;&lt; L"¡Hola!" &lt;&lt; endl;
  </code></pre>
  
  <p><strong>EDIT</strong>
  I could not find any other solution than to wrap this snippet around in a stream.
  Hope, somebody has better ideas.</p>
  
  <pre><code>//Unicode output for a Windows console 
  ostream &amp;operator-(ostream &amp;stream, const wchar_t *s) 
  { 
      int bufSize = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL);
      char *buf = new char[bufSize];
      WideCharToMultiByte(CP_UTF8, 0, s, -1, buf, bufSize, NULL, NULL);
      wprintf(L"%S", buf);
      delete[] buf; 
      return stream; 
  } 
  
  ostream &amp;operator-(ostream &amp;stream, const wstring &amp;s) 
  { 
      stream - s.c_str();
      return stream; 
  } 
  </code></pre>