title: Temporaries optimization in chaining operator
body: <p>I have an <code>Array</code> class <strong>which is expensive to construct a new instance</strong>. I defined <code>operator+</code> for it to be able to chain up like this.</p>
  <pre class="lang-cpp prettyprint-override"><code>class Array
  {
  public:
      //...
      Array&amp; operator+=(Array const&amp;);
      Array operator+(Array const&amp;);
  };
  
  Array a, b, c, d;
  Array e = a + b + c + d; //a+b-&gt;Temp, Temp+c-&gt;Temp2, Temp2+d-&gt;e, BAD!
  </code></pre>
  <p>I want it to be able to use <code>operator+=</code> which saves the cost of constructing a new instance, instead of using <code>operator+</code> which will construct a new instance. I tried using reference qualifier like this:</p>
  <pre class="lang-cpp prettyprint-override"><code>Array operator+(Array const&amp;) &amp;;
  Array&amp; operator+(Array const&amp;) &amp;&amp;; //call += internally
  </code></pre>
  <p>But after <code>Temp+c-&gt;Temp</code>, <code>Temp+d</code> will call <code>operator+() &amp;</code> which still create new instance.</p>
  <p>EDIT: I tried</p>
  <pre class="lang-cpp prettyprint-override"><code>Array&amp;&amp; operator+(Array const&amp;) &amp;&amp;
  {
      //...
      return std::move(*this);
  }
  </code></pre>
  <p>Which seems to work, but will it dangle? Because <code>Array e = a + b + c + d;</code> will call move constructor to move the final temporary into <code>e</code> and I can do some tricks in the move constructor to prevent the resource being freed when the temporary is destoried after the expression, if I am correct.</p>