Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to inform C++ (specifically <code>std::vector</code>) that your move constructor and destructor does not throw, using <code>noexcept</code>. Then the move constructor will be called when the vector grows. </p> <p>This is how to declare and implement a move constuctor which is respected by <code>std::vector</code>:</p> <pre><code>A(A &amp;&amp; rhs) noexcept { std::cout &lt;&lt; "i am the move constr" &lt;&lt;std::endl; ... some code doing the move ... m_value=std::move(rhs.m_value) ; // etc... } </code></pre> <p>If the constructor is not <code>noexcept</code>, <code>std::vector</code> can't use it, since then it can't ensure the exception guarantees demanded by the standard.</p> <p>For more about what's said in the standard, read <a href="https://stackoverflow.com/questions/4732084/c-move-semantics-and-exceptions">C++ Move semantics and Exceptions</a> </p> <p>Credit to Bo who hinted that it may have to do with exceptions. Also consider Kerrek SB's advice and use <code>emplace_back</code> when possible. It <em>can</em> be faster (but often is not), it can be clearer and more compact, but there are also some pitfalls (especially with non-explicit constructors).</p> <p><strong>Edit</strong>, often the default is what you want: move everything that can be moved, copy the rest. To explicitly ask for that, write </p> <pre><code>A(A &amp;&amp; rhs) = default; </code></pre> <p>Doing that, you will get noexcept when possible: <a href="https://stackoverflow.com/questions/18653726/is-the-default-move-constructor-defined-as-noexcept">Is the default Move constructor defined as noexcept?</a></p> <p>Note that early versions of Visual Studio 2015 and older did not support that, even though it supports move semantics.</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