Why is += valid temporaries in standard library? <p>When I attempt to compile the following on <a href="http://ideone.com/BaQoe" rel="nofollow">ideone</a>:</p> <pre><code>class X { public: friend X&amp; operator+=(X&amp; x, const X&amp; y); }; X&amp; operator+=(X&amp; x, const X&amp; y) { return x; } int main() { X() += X(); } </code></pre> <p>As expected, this throws a compile error, because you can't pass a temporary to a non const reference.</p> <p>However, the following compiles successfully on <a href="http://ideone.com/PECyy" rel="nofollow">ideone</a>:</p> <pre><code>std::string() += std::string(); </code></pre> <p>Shouldn't this error like my example above? </p> <p><strong>Edit:</strong></p> <p>If std::string() defines <code>+=</code> as a member operation, why does it do this when such usage allows the left hand side to be a temporary? Why not define it as I have above and avoid the reference to temporary issues?</p>