title: How can I keep track of the number of bytes written to an std::ostream object?
body: <p>I want to write a bunch of data to an <code>ostream</code> object and return the number of bytes written. For example:</p>
  
  <pre><code>using namespace std;
  size_t writeStuffToStream(ostream &amp;stream)
  {
      stream &lt;&lt; some_string &lt;&lt; some_integer &lt;&lt; some_other_arbitrary_object &lt;&lt; endl;
      return number_of_bytes_written;
  }
  </code></pre>
  
  <p>There is the obvious workaround of writing everything to a <code>stringstream</code> and getting the byte count out of that, and then writing the <code>stringstream</code> to the stream, but that takes extra time and memory.</p>
  
  <p>I also realize that if if all the data I wanted to write were preexisting strings, then there would be no problem. It's <code>some_integer</code> and <code>some_other_arbitrary_object</code> that are the problem.</p>