Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Nope <code>move</code> doesn't take an rvalue-reference, it takes what has been dubbed a <em>universal reference</em> by the community. Template parameters being type-deduced behave according to the rules of <em>reference collapsing</em>. This means:</p> <ul> <li>if <code>T</code> is <code>K</code>, then <code>T&amp;&amp;</code> will simply be <code>K&amp;&amp;</code>;</li> <li>if <code>T</code> is <code>K&amp;</code>, then <code>T&amp;&amp;</code> will collapse to <code>K&amp;</code>;</li> <li>if <code>T</code> is <code>K&amp;&amp;</code>, then <code>T&amp;&amp;</code> will collapse to <code>T&amp;&amp;</code>.</li> </ul> <p>It's like a logical-AND of the <code>&amp;</code> and the <code>&amp;&amp;</code> where <code>&amp;</code> is 0 and <code>&amp;&amp;</code> is 1:</p> <pre><code> &amp; &amp;&amp; |-----------| &amp; | &amp; | &amp; | |-----|-----| &amp;&amp; | &amp; | &amp;&amp; | |-----------| </code></pre> <p>And that's how <code>move</code> works for both rvalues and lvalues.</p> <p>Examples:</p> <pre><code>template&lt;typename T&gt; void f(T&amp;&amp;); f&lt;int&gt; // T is int; plugging int into T makes int&amp;&amp; which is just int&amp;&amp; f&lt;int&amp;&gt; // T is int&amp;; plugging int&amp; into T is int&amp; &amp;&amp; which collapse to int&amp; f&lt;int&amp;&amp;&gt; // T is int&amp;&amp;; plugging int&amp;&amp; into T is int&amp;&amp; &amp;&amp; which collapse to int&amp;&amp; </code></pre> <p>Note that reference collapsing only happens with template parameters; you can't directly type <code>int&amp;&amp; &amp;&amp;</code> and expect it to compile. Of course, you don't specify types manually like that. Those are just to show what references collapse to.</p> <p>So you'd really call it like this:</p> <pre><code>int i; f(i); // T is int&amp;; int&amp; &amp;&amp; collapses to int&amp; f(4); // T is int&amp;&amp;; int&amp;&amp; &amp;&amp; collapses to int&amp;&amp; </code></pre> <p>Reference collapsing is also the reason why <code>move</code> doesn't return a <code>T&amp;&amp;</code>: the references would collapse if <code>T</code> were an lvalue reference and make <code>move</code> just return an lvalue reference. You do <code>remove_reference</code> to get to a non-reference type so that the <code>&amp;&amp;</code> will really mean "rvalue-reference".</p> <p>You can learn more here: <a href="http://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers" rel="nofollow">http://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers</a></p>
    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.
    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