title: Setting the internal buffer used by a standard stream (pubsetbuf)
body: <p>I'm writing a subroutine that needs to write data to an existing buffer, and I would like to use the <a href="http://en.cppreference.com/w/cpp/io/basic_stringstream" rel="noreferrer"><code>stringstream</code></a> class to facilitate the formatting of the data.</p>
  
  <p>Initially, I used the following code to copy the contents of the stream into the buffer, but would like to avoid this solution as it copies too much data.</p>
  
  <pre><code>#include &lt;sstream&gt;
  #include &lt;algorithm&gt;
  
  void FillBuffer(char* buffer, unsigned int size)
  {
      std::stringstream message;
      message &lt;&lt; "Hello" &lt;&lt; std::endl;
      message &lt;&lt; "World!" &lt;&lt; std::endl;
  
      std::string messageText(message.str());
      std::copy(messageText.begin(), messageText.end(), buffer);
  }
  </code></pre>
  
  <p>This is when I discovered the <a href="http://en.cppreference.com/w/cpp/io/basic_streambuf/pubsetbuf" rel="noreferrer"><code>streambuf::pubsetbuf()</code></a> method and simply rewrote the above code as follows.</p>
  
  <pre><code>#include &lt;sstream&gt;
  
  void FillBuffer(char* buffer, unsigned int size)
  {
      std::stringstream message;
      message.rdbuf()-&gt;pubsetbuf(buffer, size);
  
      message &lt;&lt; "Hello" &lt;&lt; std::endl;
      message &lt;&lt; "World!" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Unfortunately, this does not work under the C++ standard library implementation that ships with Visual Studio 2008; <code>buffer</code> remains unchanged.</p>
  
  <p>I looked at the implementation of <code>pubsetbuf</code> and it turns out that it literally "does nothing".</p>
  
  <pre><code>virtual _Myt *__CLR_OR_THIS_CALL setbuf(_Elem *, streamsize)
  {   // offer buffer to external agent (do nothing)
      return (this);
  }
  </code></pre>
  
  <p>This appears to be a limitation of the given C++ standard library implementation.  What is the recommended way to configure a stream to write its contents to a given buffer?</p>