Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(N)RVO is one of the easiest to implement optimizations. In most calling conventions for return by value the caller reserves the space for the returned object and then passes a hidden pointer to the function. The function then <em>constructs</em> the object in the address that is given. That is, <code>kk += ii + jj;</code> is translated into something like:</p> <pre><code>Integer __tmp; // __rtn this arg Integer::operator+( &amp;tmp, &amp;ii, jj ); kk += __tmp; </code></pre> <p>The function (in this case <code>Integer::operator+</code> takes a first hidden argument <code>__rtn</code> that is a pointer to an uninitialized block of memory of <code>sizeof(Integer)</code> bytes, where the object is to be constructed, a second hidden argument <code>this</code>, and then the argument to the function in the code.</p> <p>Then the implementation of the function is translated into:</p> <pre><code>Integer::operator+( Integer* __rtn, Integer const * this, const Integer &amp;rv) { cout &lt;&lt; "operator+" &lt;&lt; endl; new (__rtn) Integer(i + rv.i); __rtn-&gt;print(); } </code></pre> <p>Because the calling convention passes the pointer, there function does not need to reserve extra space for a local integer that would then be copied, as it can just build the <code>I</code> in your code straight into the received pointer, and avoid the copy.</p> <p>Note that not in all circumstances the compiler can perform NRVO, in particular, if you have two local objects in the function and you return either one depending on a condition that is not inferable from the code (say the value of an argument to the function). While you could do that to avoid RVO, the fact is that it will make your code more complex, less efficient and harder to maintain.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload