Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Despite what others have said, the standard only talks about rvalue references.</p> <p>The key to how this works for std::move is an explicit special rule in the rules for template argument deduction:</p> <blockquote> <p>[...] If [the declared function parameter type] is an rvalue reference to a cv-unqualified template parameter and the argument is an lvalue, the type “lvalue reference to A” is used in place of A for type deduction.[...]</p> </blockquote> <p>The other part are the rules for reference collapsing, which say that </p> <blockquote> <p>If [...] a type template-parameter [...] denotes a type TR that is a reference to a type T, an attempt to create the type “lvalue reference to cv TR” creates the type “lvalue reference to T”, while an attempt to create the type “rvalue reference to cv TR” creates the type TR.</p> </blockquote> <p>Now in <code>template&lt;class T&gt; typename remove_reference&lt;T&gt;::type&amp;&amp; std::move(T&amp;&amp; a);</code> the function parameter a matches above rule ("rvalue reference to cv-unqualified template parameter"), so the deduced type will be an lvalue reference to the argument type, if the argument is an lvalue. In your case that leads to T = A&amp;.</p> <p>Substituting that into the declaration of move yields </p> <pre><code>remove_reference&lt;A&amp;&gt;::type&amp;&amp; std::move&lt;A&amp;&gt;(A&amp; &amp;&amp; a); </code></pre> <p>Using the definition of remove_reference and the reference collapsing rule (rvalue reference to TR => TR), makes this:</p> <pre><code>A&amp;&amp; std::move&lt;A&amp;&gt;(A&amp; a); </code></pre> <p>Scott Meyer's <em>universal reference</em> concept, as put forward in other answers, is a helpful way to remember this surprising effect of the combination of the rules for type deduction and of reference collapsing: rvalue references to a deduced type may end up being lvalue references (if the type may be deduced to be a lvalue reference). But there are no <em>universal references</em> int the standard. As Scott Meyers says: it is a lie - but a lie that is more helpful than the truth...</p> <p>Note that std::forward is a different twist on this theme: it uses an extra indirection to prevent argument deduction (so that the type must be given explicitly), but also uses reference collapsing to forward lvalues as lvalues and rvalues as rvalues.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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