title: How can I use std::imbue to set the locale for std::wcout?
body: <p>I am trying to use the <code>std::locale</code> mechanism in C++11 to count words in different languages.  Specifically, I have <code>std::wstringstream</code> which contains the title of a famous Russian novel (&quot;Crime and Punishment&quot; in English).  What I want to do is to use the appropriate locale (<code>ru_RU.utf8</code> on my Linux machine) to read the stringstream, count the words and print the results. I should also probably note that my system is set to use the <code>en_US.utf8</code> locale.</p>
  <p>The desired result is this:</p>
  <pre><code>0: &quot;Преступление&quot;
  1: &quot;и&quot;
  2: &quot;наказание&quot;
  
  I counted 3 words.
  and the last word was &quot;наказание&quot;
  </code></pre>
  <p>That all works when I set the global locale, but not when I attempt to <code>imbue</code> the <code>wcout</code> stream.  When I try that, I get this result instead:</p>
  <pre><code>0: &quot;????????????&quot;
  1: &quot;?&quot;
  2: &quot;?????????&quot;
  
  I counted 3 words.
  and the last word was &quot;?????????&quot;
  </code></pre>
  <p>Also, when I attempt to use a solution suggested in the comments, (which can be activate by changing <code>#define USE_CODECVT 0</code> to <code>#define USE_CODECVT 1</code>) I get the error mentioned in <a href="https://stackoverflow.com/questions/15615136/is-codecvt-not-a-std-header">this other question</a>.</p>
  <p>Those interested in experimenting with the code, or with compiler settings or both may wish to use <a href="http://coliru.stacked-crooked.com/a/6d7bc409f511b0ae" rel="noreferrer">this live code</a>.</p>
  <h2>My questions</h2>
  <ol>
  <li>Why does that not work?  Is it because <code>wcout</code> is already open?</li>
  <li>Is there way to use <code>imbue</code> rather than setting the global locale to do what I want?</li>
  </ol>
  <p>If it makes a difference, I'm using g++ 4.8.3. The full code is shown below.</p>
  <h2>getwords.cpp</h2>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  #include &lt;locale&gt;
  
  #define USE_CODECVT 0
  #define USE_IMBUE   1
  
  #if USE_CODECVT
  #include &lt;codecvt&gt;
  #endif 
  using namespace std;
  
  int main()
  {
  #if USE_CODECVT
      locale ru(&quot;ru_RU.utf8&quot;, 
          new codecvt_utf8&lt;wchar_t, 0x10ffff, consume_header&gt;{});
  #else
      locale ru(&quot;ru_RU.utf8&quot;);
  #endif
  #if USE_IMBUE
      wcout.imbue(ru);
  #else
      locale::global(ru);
  #endif
      wstringstream in{L&quot;Преступление и наказание&quot;};
      in.imbue(ru);
      wstring word;
      unsigned wordcount = 0;
      while (in &gt;&gt; word) {
          wcout &lt;&lt; wordcount &lt;&lt; &quot;: \&quot;&quot; &lt;&lt; word &lt;&lt; &quot;\&quot;\n&quot;;
          ++wordcount;
      }
      wcout &lt;&lt; &quot;\nI counted &quot; &lt;&lt; wordcount &lt;&lt; &quot; words.\n&quot;
          &lt;&lt; &quot;and the last word was \&quot;&quot; &lt;&lt; word &lt;&lt; &quot;\&quot;\n&quot;;
  }
  </code></pre>