Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Altough both, <code>return std::move(local)</code> and <code>return local</code>, do work in sense of that they do compile, their behavior is different. And probably only the latter one was intended.</p> <p>If you write a function which returns a <code>std::vector&lt;string&gt;</code>, you have to return a <code>std::vector&lt;string&gt;</code> and exactly it. <code>std::move(local)</code> has the type<code>std::vector&lt;string&gt;&amp;&amp;</code> which is <strong>not</strong> a <code>std::vector&lt;string&gt;</code> so it has to be converted to it using the move constructor.</p> <p>The standard says in 6.6.3.2:</p> <blockquote> <p>The value of the expression is implicitly converted to the return type of the function in which it appears.</p> </blockquote> <p>That means, <code>return std::move(local)</code> is equalvalent to</p> <pre><code>std::vector&lt;std::string&gt; converted(std::move(local); // move constructor return converted; // not yet a copy constructor call (which will be elided anyway) </code></pre> <p>whereas <code>return local</code> only is</p> <pre><code>return local; // not yet a copy constructor call (which will be elided anyway) </code></pre> <p>This spares you one operation.</p> <hr> <p>To give you a short example of what that means:</p> <pre><code>struct test { test() { std::cout &lt;&lt; " construct\n"; } test(const test&amp;) { std::cout &lt;&lt; " copy\n"; } test(test&amp;&amp;) { std::cout &lt;&lt; " move\n"; } }; test f1() { test t; return t; } test f2() { test t; return std::move(t); } int main() { std::cout &lt;&lt; "f1():\n"; test t1 = f1(); std::cout &lt;&lt; "f2():\n"; test t2 = f2(); } </code></pre> <p>This will output</p> <pre><code>f1(): construct f2(): construct move </code></pre>
    singulars
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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