Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn value optimizations and side-effects
    primarykey
    data
    text
    <p>Return value optimization (RVO) is an optimization technique involving copy elision, which eliminates the temporary object created to hold a function's return value in certain situations. I understand the benefit of RVO in general, but I have a couple of questions. </p> <p>The standard says the following about it in §12.8, paragraph 32 of <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf">this working draft</a> (emphasis mine).</p> <blockquote> <p>When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, <strong>even if the copy/move constructor and/or destructor for the object have side effects</strong>. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.</p> </blockquote> <p>It then lists a number of criteria when the implementation may perform this optimization. </p> <hr> <p>I have a couple of questions regarding this potential optimization:</p> <ol> <li><p>I am used to optimizations being constrained such that they cannot change observable behaviour. This restriction does not seem to apply to RVO. <strong>Do I ever need to worry about the side effects mentioned in the standard?</strong> Do corner cases exist where this might cause trouble?</p></li> <li><p><strong>What do I as a programmer need to do (or not do) to allow this optimization to be performed?</strong> For example, does the following prohibit the use of copy elision (due to the <code>move</code>):</p></li> </ol> <pre class="lang-c prettyprint-override"><code>std::vector&lt;double&gt; foo(int bar){ std::vector&lt;double&gt; quux(bar,0); return std::move(quux); } </code></pre> <h2>Edit</h2> <p>I posted this as a new question because the specific questions I mentioned are not directly answered in other, related questions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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