Is this an instance of a reference-to-non-const binding to a temporary?
<p><sub><strong>Disclaimer:</strong> This question is for understanding. I'll use <code>boost::lexical_cast</code> in the field. <a href="http://codereview.stackexchange.com/questions/6094/a-version-of-operator-that-returns-ostringstream-instead-of-ostream">It <em>has</em> sort of come up in the real world in places, though.</a></sub></p>

<hr>

<p>Take <a href="https://ideone.com/XfMFOx" rel="nofollow">the following attempt</a> at an "inline" lex-cast approach:</p>

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

int main()
{
    const std::string s = static_cast&lt;std::ostringstream&amp;&gt;(
       std::ostringstream() &lt;&lt; "hi" &lt;&lt; 0
    ).str();
    std::cout &lt;&lt; s;
}
</code></pre>

<p>The result is something like <code>0x804947c0</code>, because the <code>operator&lt;&lt;</code> that works with <code>"hi"</code> is a free function whose LHS must take <code>std::ostream&amp;</code><sup>&dagger;</sup>, and temporary <code>std::ostringstream()</code> can't bind to a ref-to-non-<code>const</code>. The only remaining match is the <code>operator&lt;&lt;</code> that takes <code>const void*</code> on the RHS<sup>&dagger;&dagger;</sup>.</p>

<p>Now <a href="https://ideone.com/cHUQUP" rel="nofollow">let's swap the operands</a>:</p>

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

int main()
{
    const std::string s = static_cast&lt;std::ostringstream&amp;&gt;(
       std::ostringstream() &lt;&lt; 0 &lt;&lt; "hi"
    ).str();
    std::cout &lt;&lt; s;
}
</code></pre>

<p>The result is <code>"0hi"</code>.</p>

<p>This mostly makes sense, because the <code>operator&lt;&lt;</code> that takes <code>int</code> is a member function of base <code>ostream</code><sup>&dagger;&dagger;&dagger;</sup> and, as such, is fine with being invoked on the temporary. The result of that operation is a reference to the <code>ostream</code> base, to which the next <code>operator&lt;&lt;</code> is chained, i.e. read it as <code>(std::ostringstream() &lt;&lt; 0) &lt;&lt; "hi"</code>.</p>

<p>But why then does <em>that</em> operation on <code>"hi"</code> go on to yield the expected result? <strong>Isn't the reference on the LHS still a temporary?</strong></p>

<hr>

<p><sub>Let's focus on C++03; I'm told that the first example may actually work as "intended" in C++11 due to the catch-all operator for rvalues.</sub></p>

<p><sup><sup>&dagger;</sup> <code>[C++03: 27.6.2.1]: template&lt;class charT, class traits&gt;
basic_ostream&lt;charT,traits&gt;&amp; operator&lt;&lt;(basic_ostream&lt;charT,traits&gt;&amp;,charT*);</code></sup></p>

<p><sup><sup>&dagger;&dagger;</sup> <code>[C++03: 27.6.2.1]: basic_ostream&lt;charT,traits&gt;&amp; operator&lt;&lt;(const void* p);</code></sup></p>

<p><sup><sup>&dagger;&dagger;&dagger;</sup> <code>[C++03: 27.6.2.1]: basic_ostream&lt;charT,traits&gt;&amp; operator&lt;&lt;(int n);</code></sup></p>
