Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The compiler simply has to work consistently.</p> <p>In other words, the compiler has to look solely at a return <em>type</em>, and based on that type, decide how a function returning an object of that type will return the value.</p> <p>At least in a typical case, that decision is <em>fairly</em> trivial. It sets aside a register (or possibly two) to use for return values (e.g., on an Intel/AMD x86/x64 that'll normally be EAX or RAX). Any type small enough to fit into that will be returned there. For any type too large to fit there, the function will receive a hidden pointer/reference parameter that tells it where to deposit the return result. Note that this much applies without RVO/NRVO being involved at all -- in fact, it applies equally to C code that returns a <code>struct</code> as it does to C++ returning a <code>class</code> object. Although returning a <code>struct</code> probably isn't quite as common in C as in C++, it's still allowed, and the compiler has to be able to compile code that does it.</p> <p>There are really two separate (possible) copies that can be eliminated. One is that the compiler may allocate space on the stack for a local holding what will be the return value, then copy from there to where the pointer refers during the return.</p> <p>The second is a possible copy from that return address into some other location where the value really needs to end up.</p> <p>The first gets eliminated inside the function itself, but has no effect on its external interface. It ultimately puts the data wherever the hidden pointer tells it to -- the only question is whether it creates a local copy first, or always works directly with the return point. Obviously with [N]RVO, it always works directly.</p> <p>The second possible copy is from that (potential) temporary into wherever the value really needs to end up. This is eliminated by optimizing the calling sequence, not the function itself -- i.e., giving the function a pointer to the final destination for that return value, rather than to some temporary location, from which the compiler will then copy the value into its destination.</p>
 

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