title: Assignment of an operation without intermediate object
body: <p>Given a class like this:</p>
  
  <pre><code>class Vec{
    int comp[2];
  public:
    void add(Vec&amp; vec, Vec&amp; vec2){
      comp[0] = vec.comp[0] + vec2.comp[0];
      comp[1] = vec.comp[1] + vec2.comp[1];
    }
    Vec operator+ (Vec&amp; vec){
      Vec res;
      res.comp[0] = comp[0] + vec.comp[0];
      res.comp[1] = comp[1] + vec.comp[1];
      return res;
    }
  };
  </code></pre>
  
  <p>There are two member functions that essentially do the same thing, which is add two <code>Vec</code>s together. The difference, of course, is that there are no intermediate values involved in the <code>add</code> function, whereas <code>operator+</code> declares a local object of <code>Vec</code>.</p>
  
  <p>My question is, is there any way to define <code>operator+</code> in combination with <code>operator=</code> to have the same semantics as <code>add</code>, having no intermediate values? The rationale is to decrease the amount of intermediate values involved, for efficiency, yet keep the elegance of the <code>operator</code> syntax.</p>
  
  <p>The C++ interface for the GMP library apparently is able to do this: <a href="http://gmplib.org/manual/C_002b_002b-Interface-General.html#C_002b_002b-Interface-General" rel="nofollow">http://gmplib.org/manual/C_002b_002b-Interface-General.html#C_002b_002b-Interface-General</a></p>
  
  <blockquote>
    <p>An important feature of the implementation is that an expression like a=b+c results in a single call to the corresponding mpz_add, without using a temporary for the b+c part.</p>
  </blockquote>
  
  <p>I'm wondering if there is some possible way to do this, or would GMP have to use some sort of workaround for this?</p>