Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>When, where, and why should we use <code>BigObject&amp;&amp; rv = std::move(big_obj);</code>?</strong></p> <p>Short answer: never, nowhere, and for the following reason:</p> <p>All named variable expressions are lvalues. So what this is doing is taking <code>big_obj</code>, which is an lvalue, coercing it into an xvalue, and using that to initialize an rvalue reference which can then only be used as an lvalue, and we're back to where we started. It's a completely useless line of code.</p> <p>Except for function arguments, defining local rvalue references is generally not very useful. They do extend the lifetimes of the temporary objects used to initialize them, though, so they might find occasional use in separating complex expressions across multiple statements.</p> <p>Value category is a property of expressions, not of references. You choose which kind of reference to use based on the value category of the expressions you want it to be able to bind to. And you use <code>std::move</code> to force a particular overload of a function to be called where it would otherwise call the wrong one or be ambiguous.</p> <p>Bjarne's advice given during a Q&amp;A session at GoingNative2013 was basically, don't try to use rvalue references for anything more "clever" than move constructors and move assignment operators.</p> <p>And from Herb Sutter: A lot of people think move means writing <code>&amp;&amp;</code> everywhere, but it's not. You don't need to write <code>&amp;&amp;</code> yourself, unless you're writing a move constructor or move assignment operator, or perfectly forwarding an argument in a function template.</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. 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