Why doesn't the string class have a << operator (operator<<) predefined so that strings work like ostringstreams?
<p>It seems to me that defining the &lt;&lt; operator (operator&lt;&lt;) to work directly with strings is more elegant than having to work with ostringstreams and then converting back to strings. Is there a reason why c++ doesn't do this out of the box?</p>

<pre><code>#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;
using namespace std;

template &lt;class T&gt; 
string&amp; operator&lt;&lt;(string&amp; s, T a) {
    ostringstream ss;
    ss &lt;&lt; a;
    s.append(ss.str());
    return s;
}
int main() {
    string s;
    // this prints out: "inserting text and a number(1)"
    cout &lt;&lt; (s &lt;&lt; "inserting text and a number (" &lt;&lt; 1 &lt;&lt; ")\n");

    // normal way
    ostringstream os; 
    os &lt;&lt; "inserting text and a number(" &lt;&lt; 1 &lt;&lt; ")\n";
    cout &lt;&lt; os.str();
}
</code></pre>
