Stream object directly into a std::string : <p>Given some type that is streamable:</p>

<pre><code>struct X {
    int i;

    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, X const&amp; x) {
        return os &lt;&lt; "X(" &lt;&lt; x.i &lt;&lt; ')';
    }
};
</code></pre>

<p>I want to append this onto a <code>std::string</code>. I can implement this as:</p>

<pre><code>void append(std::string&amp; s, X const&amp; x) {
    std::ostringstream os;
    os &lt;&lt; x;
    s.append(os.str());
}
</code></pre>

<p>But this seems lame since I'm writing data into one stream just to then allocate a new string just for the purposes of appending it onto a different one. Is there a more direct route? </p>
