Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The code is right. <code>auto&amp;&amp; p = expr</code> means the type of <code>p</code> is <code>T&amp;&amp;</code> where <code>T</code> will be inferred from <code>expr</code>. The <code>&amp;&amp;</code> here indicates a rvalue reference, so e.g.</p> <pre><code>auto&amp;&amp; p = 1; </code></pre> <p>will infer <code>T == int</code> and thus the type of <code>p</code> is <code>int&amp;&amp;</code>.</p> <p>However, references can be collapsed according to the rule:</p> <pre><code>T&amp; &amp; == T&amp; T&amp; &amp;&amp; == T&amp; T&amp;&amp; &amp; == T&amp; T&amp;&amp; &amp;&amp; == T&amp;&amp; </code></pre> <p>(This feature is used to implement perfect forwarding in C++11.)</p> <p>In the case</p> <pre><code>auto&amp;&amp; p = x; </code></pre> <p>as <code>x</code> is an lvalue, an rvalue reference cannot be bound to it, but if we infer <code>T = int&amp;</code> then the type of <code>p</code> will become <code>int&amp; &amp;&amp; = int&amp;</code>, which is an lvalue reference, which can be bound to <code>x</code>. Only in this case <code>auto&amp;&amp;</code> and <code>auto&amp;</code> give the same result. These two are different though, e.g.</p> <pre><code>auto&amp; p = std::move(x); </code></pre> <p>is incorrect because <code>std::move(x)</code> is an rvalue, and the lvalue reference cannot be bound to it.</p> <p>Please read <a href="http://thbecker.net/articles/rvalue_references/section_01.html">C++ Rvalue References Explained </a> for a walk through.</p>
 

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