title: Custom Stringstream - Convert std::wstring & std::string
body: <p>I've got a template class derived from <code>std::basic_stringstream&lt;typename TString::value_type...&gt;</code>, as you can see. The problem happens while trying to convert them. It's probably an obvious problem, though I cannot seem to figure out the solution.</p>
  
  <p>As example in <code>main</code>, I have a simple <code>std::wstring</code> and initialize it with <code>L"123"</code>.<br/>
  After the <code>std::wstring</code> has been constructed, the operator of the custom <code>basic_stringstream</code> class is called (depending on <code>std::wstring</code> or <code>std::string</code>).</p>
  
  <p>Inspecting the <code>WCStringStream</code> object for debugging purposes, shows that it contains - instead of the string <code>L"123"</code>, the address of the first element of the entered string. The functions <code>to_bytes</code> and <code>from_bytes</code> do return the correct converted string, so the only problem left is the operator being called in both operator-functions:</p>
  
  <pre><code>*this &lt;&lt; std::wstring_convert&lt;...&gt;().xx_bytes(s);
  </code></pre>
  
  <p><strong><em>Example:</em></strong><br/>
  Template class is <code>std::wstring</code>.<br/>
  Input is a <code>std::string</code>.<br/>
  <code>&amp;operator&lt;&lt;(const std::string &amp;s)</code> is being called.<br/>
  String is converted.<br/>
  <code>&amp;operator&lt;&lt;(const std::wstring &amp;s)</code> is being called.<br/>
  String-type matches with template type.<br/>
  Operator of base-class (<code>basic_stringstream</code>) is called. (Or <code>std::operator...</code>)</p>
  
  <p><strong><em>Result:</em></strong><br/>
  Inspecting: <code>{_Stringbuffer={_Seekhigh=0x007f6808 L"003BF76C췍췍췍췍췍췍췍췍췍...}...}</code><br/>
  <code>WCStringStream&lt;std::wstring&gt;::str()</code> <strong>-></strong> <code>"003BF76C"</code></p>
  
  <p><strong><em>Expected result:</em></strong><br/>
  <code>"123"</code></p>
  
  <p>What's going wrong here ?</p>
  
  <hr>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  #define NOMINMAX
  #include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;codecvt&gt;
  
  template&lt;class TString&gt;
  class WCStringStream : public std::basic_stringstream&lt;typename TString::value_type,
      std::char_traits&lt;typename TString::value_type&gt;,
      std::allocator&lt;typename TString::value_type&gt; &gt;
  {
      typedef typename TString::value_type CharTraits;
      typedef std::basic_stringstream&lt;CharTraits, std::char_traits&lt;CharTraits&gt;, std::allocator&lt;CharTraits&gt; &gt; MyStream;
      //more typedefs...
  
  public:
      //Constructor...
      inline WCStringStream(void) { }
      inline WCStringStream(const TString &amp;s) : MyStream(s) { }
      //and more...
      //operator&gt;&gt; overloads...
      //defines for VS2010/2015 (C++11) included
  
      inline WCStringStream &amp;operator&lt;&lt;(const std::wstring &amp;s)
      {
          if (typeid(TString) == typeid(s))
              MyStream::operator&lt;&lt;(s.c_str());
          else
              *this &lt;&lt; std::wstring_convert&lt;std::codecvt_utf8&lt;wchar_t&gt;, wchar_t&gt;().to_bytes(s);
          return *this;
      }
  
      inline WCStringStream &amp;operator&lt;&lt;(const std::string &amp;s)
      {
          if (typeid(TString) == typeid(s))
              MyStream::operator&lt;&lt;(s.c_str());
          else
              *this &lt;&lt; std::wstring_convert&lt;std::codecvt_utf8&lt;wchar_t&gt;, wchar_t&gt;().from_bytes(s);
          return *this;
      }
  };
  
  //Example main
  int main(int argc, char *argv[])
  {
      typedef std::wstring fstring;
  
      WCStringStream&lt;std::wstring&gt; ws;
      WCStringStream&lt;std::string&gt; ss;
  
      ws &lt;&lt; fstring(L"123");
      int a = 0;
      ws &gt;&gt; a;
      std::cout &lt;&lt; a &lt;&lt; std::endl;
  
      ss &lt;&lt; fstring(L"123");
      int b = 0;
      ss &gt;&gt; b;
      std::cout &lt;&lt; b &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p><em>I'm compiling currently in VS2015 but I'd need it to run on VS2010 too.</em></p>