Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <pre><code>return(theVector); </code></pre> </blockquote> <p>This already moves implicitly due to a special language rule, because <code>theVector</code> is a local object. See section 12.8 paragraphs 34 and 35:</p> <blockquote> <p><strong>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. 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. This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):</p> <p>— <strong>in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type</strong>, the copy/move operation can be omitted by constructing the automatic object directly into the function's return value</p> <p>[...]</p> <p><strong>When the criteria for elision of a copy operation are met and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue</strong>.</p> </blockquote> <p>Note that you must return a <code>std::vector&lt;T&gt;</code> (<em>by value</em>), <strong>not</strong> a <code>std::vector&lt;T&gt;&amp;&amp;</code> (<em>by reference</em>).</p> <p>But why the parenthesis? <code>return</code> is not a function:</p> <blockquote> <pre><code>return theVector; </code></pre> </blockquote>
 

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