Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It declares an <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1690.html" rel="noreferrer">rvalue reference</a> (standards proposal doc).</p> <p>Here's an introduction to rvalue <a href="http://www.artima.com/cppsource/rvalue.html" rel="noreferrer">references</a>. </p> <p>Here's a fantastic in-depth look at rvalue references by one of Microsoft's standard library <a href="http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx" rel="noreferrer">developers</a>. (But see the Caution in the comments following this answer before reading this article.)</p> <p>The biggest difference between a C++03 reference (now called an lvalue reference in C++11) is that it can bind to an rvalue like a temporary without having to be const. Thus, this syntax is now legal:</p> <pre><code>T&amp;&amp; r = T(); </code></pre> <p>rvalue references primarily provide for the following:</p> <p><strong>Move semantics</strong>. A move constructor and move assignment operator can now be defined that takes an rvalue reference instead of the usual const-lvalue reference. A move functions like a copy, except it is not obliged to keep the source unchanged; in fact, it usually modifies the source such that it no longer owns the moved resources. This is great for eliminating extraneous copies, especially in standard library implementations.</p> <p>For example, a copy constructor might look like this:</p> <pre><code>foo(foo const&amp; other) { this-&gt;length = other.length; this-&gt;ptr = new int[other.length]; copy(other.ptr, other.ptr + other.length, this-&gt;ptr); } </code></pre> <p>If this constructor was passed a temporary, the copy would be unnecessary because we know the temporary will just be destroyed; why not make use of the resources the temporary already allocated? In C++03, there's no way to prevent the copy as we cannot determine we were passed a temporary. In C++11, we can overload a move constructor:</p> <pre><code>foo(foo&amp;&amp; other) { this-&gt;length = other.length; this-&gt;ptr = other.ptr; other.length = 0; other.ptr = nullptr; } </code></pre> <p>Notice the big difference here: the move constructor actually modifies its argument. This would effectively "move" the temporary into the object being constructed, thereby eliminating the unnecessary copy.</p> <p>The move constructor would be used for temporaries and for non-const lvalue references that are explicitly converted to rvalue references using the <code>std::move</code> function (it just performs the conversion). The following code both invoke the move constructor for <code>f1</code> and <code>f2</code>:</p> <pre><code>foo f1((foo())); // Move a temporary into f1; temporary becomes "empty" foo f2 = std::move(f1); // Move f1 into f2; f1 is now "empty" </code></pre> <p><strong>Perfect forwarding</strong>. rvalue references allow us to properly forward arguments for templated functions. Take for example this factory function:</p> <pre><code>template &lt;typename T, typename A1&gt; std::unique_ptr&lt;T&gt; factory(A1&amp; a1) { return std::unique_ptr&lt;T&gt;(new T(a1)); } </code></pre> <p>If we called <code>factory&lt;foo&gt;(5)</code>, the argument will be deduced to be <code>int&amp;</code>, which will not bind to a literal 5, even if <code>foo</code>'s constructor takes an <code>int</code>. Well, we could instead use <code>A1 const&amp;</code>, but what if <code>foo</code> takes the constructor argument by non-const reference? To make a truly generic factory function, we would have to overload factory on <code>A1&amp;</code> and on <code>A1 const&amp;</code>. That might be fine if factory takes 1 parameter type, but each additional parameter type would multiply the necessary overload set by 2. That's very quickly unmaintainable.</p> <p>rvalue references fix this problem by allowing the standard library to define a <code>std::forward</code> function that can properly forward lvalue/rvalue references. For more information about how <code>std::forward</code> works, see <a href="https://stackoverflow.com/a/3582313/530189">this excellent answer</a>.</p> <p>This enables us to define the factory function like this:</p> <pre><code>template &lt;typename T, typename A1&gt; std::unique_ptr&lt;T&gt; factory(A1&amp;&amp; a1) { return std::unique_ptr&lt;T&gt;(new T(std::forward&lt;A1&gt;(a1))); } </code></pre> <p>Now the argument's rvalue/lvalue-ness is preserved when passed to <code>T</code>'s constructor. That means that if factory is called with an rvalue, <code>T</code>'s constructor is called with an rvalue. If factory is called with an lvalue, <code>T</code>'s constructor is called with an lvalue. The improved factory function works because of one special rule:</p> <blockquote> <p>When the function parameter type is of the form <code>T&amp;&amp;</code> where <code>T</code> is a template parameter, and the function argument is an lvalue of type <code>A</code>, the type <code>A&amp;</code> is used for template argument deduction.</p> </blockquote> <p>Thus, we can use factory like so:</p> <pre><code>auto p1 = factory&lt;foo&gt;(foo()); // calls foo(foo&amp;&amp;) auto p2 = factory&lt;foo&gt;(*p1); // calls foo(foo const&amp;) </code></pre> <p><strong>Important rvalue reference properties</strong>:</p> <ul> <li>For overload resolution, <strong>lvalues prefer binding to lvalue references and rvalues prefer binding to rvalue references</strong>. Hence why temporaries prefer invoking a move constructor / move assignment operator over a copy constructor / assignment operator.</li> <li><strong>rvalue references will implicitly bind to rvalues and to temporaries that are the result of an implicit conversion</strong>. i.e. <code>float f = 0f; int&amp;&amp; i = f;</code> is well formed because float is implicitly convertible to int; the reference would be to a temporary that is the result of the conversion.</li> <li><strong>Named rvalue references are lvalues. Unnamed rvalue references are rvalues.</strong> This is important to understand why the <code>std::move</code> call is necessary in: <code>foo&amp;&amp; r = foo(); foo f = std::move(r);</code></li> </ul>
    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.
    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