Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that </p> <pre><code>p.emplace_back(v[i]); </code></pre> <p>passes an lvalue to <code>emplace_back</code>, which means that your move constructor (which expects an rvalue reference) won't work. </p> <p>If you actually want to move values from one container to another, you should explicitly call <a href="http://en.cppreference.com/w/cpp/utility/move" rel="nofollow"><code>std::move</code></a>:</p> <pre><code>p.emplace_back(std::move(v[i])); </code></pre> <p>(The idea behind a move constructor like <code>obj(obj&amp;&amp; tmp)</code> is that <code>tmp</code> should be an object that isn't going to be around for much longer. In your <strong>first</strong> loop, you pass a temporary object to <code>emplace_back</code>, which is fine -- a rvalue reference can bind to a temporary object and steal data from it because the temporary object is about to disappear. In your <strong>second</strong> loop, the object that you pass to <code>emplace_back</code> has a name: <code>v[i]</code>. That means it's not temporary, and could be referred to later in the program. That's why you have to use <a href="http://en.cppreference.com/w/cpp/utility/move" rel="nofollow"><code>std::move</code></a> to tell the compiler "<em>yes, I really meant to steal data from this object, even though someone else might try to use it later.</em>")</p> <hr> <p><strong>Edit:</strong> I'm assuming that your rather unusual usage of <code>emplace_back</code> is a relic of having to craft a little example for us. If that isn't the case, see @jogojapan's answer for a good discussion about why using a <code>std::vector</code> move constructor or repeated calls to <code>push_back</code> would make more sense for your example.</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.
 

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