Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although the existing answer provides a workaround using <code>std::move</code> that makes your program compile, it must be said that your use of <code>emplace_back</code> seems to be based on a misunderstanding.</p> <p>The way you describe it (<em>"I was trying to [...] moving the content from a vector to another one using <code>emplace_back()</code>"</em>) and the way you use it suggest that you think of <code>emplace_back</code> as a method to <em>move</em> elements into the vector, and of <code>push_back</code> as a method to <em>copy</em> elements into a vector. The code you use to fill the first instance of the vector seems to suggest this as well:</p> <pre><code>std::vector&lt;obj&gt; v; for( int i = 0; i &lt; 1000; ++i ) { v.emplace_back(obj("Jon")); } </code></pre> <p>But this is not what the difference between <code>emplace_back</code> and <code>push_back</code> is about.</p> <p><strong>Firstly, even <code>push_back</code> will <em>move</em> (not copy) the elements</strong> into the vector if only it is given an rvalue, and if the element type has a move assignment operator.</p> <p><strong>Secondly, the real use case of <code>emplace_back</code> is to construct elements <em>in place</em></strong>, i.e. you use it when you want to put objects into a vector that do not exist yet. The arguments of <code>emplace_back</code> are <em>the arguments to the constructor</em> of the object. So your loop above should really look like this:</p> <pre><code>std::vector&lt;obj&gt; v; for( int i = 0; i &lt; 1000; ++i ) { v.emplace_back("Jon"); // &lt;-- just pass the string "Jon" , not obj("Jon") } </code></pre> <p>The reason why your existing code works is that <code>obj("Jon")</code> is also a valid argument to the constructor (specifically, to the move constructor). But the main idea of <code>emplace_back</code> is that you need not create the object <em>and then</em> move it in. You don't benefit from that idea when you pass <code>obj("Jon")</code> instead of <code>"Jon"</code> to it.</p> <p>On the other hand, in your second loop you are dealing with objects that were created before. There is no point in using <code>emplace_back</code> to move objects that exist already. And again, <code>emplace_back</code> applied to an existing object does not mean that the object is moved. It only means that it is created in-place, using the ordinary copy constructor (if that exists). If you want to move it, simply use <code>push_back</code>, applied to the result of <code>std::move</code>:</p> <pre><code>std::vector&lt;obj&gt; p; for( int i = 0; i &lt; 1000; ++i ) { p.push_back(std::move(v[i])); // &lt;-- Use push_back to move existing elements } </code></pre> <p><strong>Further notes</strong><br> 1) You can simplify the loop above using C++11 range-based for:</p> <pre><code>std::vector&lt;obj&gt; p; for (auto &amp;&amp;obj : v) p.push_back(std::move(obj)); </code></pre> <p>2) Regardless of whether you use an ordinary for-loop or range-based for, you move the elements one by one, which means that the source vector <code>v</code> will remain as a vector of 1000 empty objects. If you actually want to clear the vector in the process (but still use move semantics to transport the elements to the new vector), you can use the move constructor of the vector itself:</p> <pre><code>std::vector&lt;obj&gt; p(std::move(v)); </code></pre> <p>This reduces the second loop to just a single line, and it makes sure the source vector is cleared.</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