Why is copy constructor chosen over move constructor
<p>I was looking at the following example regarding move constructor/assignment:
<a href="https://msdn.microsoft.com/en-us/library/dd293665.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/dd293665.aspx</a></p>

<p>I have modified it a little by adding a swap function to simplify the move constructor/assignment and the copy assignment:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

using namespace std;

class MemoryBlock
{
public:

    // Simple constructor that initializes the resource.
    explicit MemoryBlock(size_t length)
        : _length(length)
        , _data(new int[length])
    {
        std::cout &lt;&lt; "In MemoryBlock(size_t). length = "
                  &lt;&lt; _length &lt;&lt; "." &lt;&lt; std::endl;
    }

    // Destructor.
    ~MemoryBlock()
    {
        std::cout &lt;&lt; "In ~MemoryBlock(). length = "
                  &lt;&lt; _length &lt;&lt; ".";

        if (_data != nullptr)
        {
            std::cout &lt;&lt; " Deleting resource.";
            // Delete the resource.
            delete[] _data;
        }

        std::cout &lt;&lt; std::endl;
    }

    // Copy constructor.
    MemoryBlock(const MemoryBlock&amp; other)
        : _length(other._length)
        , _data(new int[other._length])
    {
        std::cout &lt;&lt; "In MemoryBlock(const MemoryBlock&amp;). length = "
                  &lt;&lt; other._length &lt;&lt; ". Copying resource." &lt;&lt; std::endl;

        std::copy(other._data, other._data + _length, _data);
    }

    // Copy assignment operator.
    MemoryBlock&amp; operator=(MemoryBlock&amp; other)
    {
        std::cout &lt;&lt; "In operator=(const MemoryBlock&amp;). length = "
                  &lt;&lt; other._length &lt;&lt; ". Copying resource." &lt;&lt; std::endl;

        swap(*this, other);
        return *this;
    }

    // Retrieves the length of the data resource.
    size_t Length() const
    {
        return _length;
    }

    // Move constructor.
    MemoryBlock(MemoryBlock&amp;&amp; other)
        : _data(nullptr)
        , _length(0)
    {
        std::cout &lt;&lt; "In MemoryBlock(MemoryBlock&amp;&amp;). length = "
                  &lt;&lt; other._length &lt;&lt; ". Moving resource." &lt;&lt; std::endl;


        *this = std::move(other);
    }

    // Move assignment operator.
    MemoryBlock&amp; operator=(MemoryBlock&amp;&amp; other)
    {
        std::cout &lt;&lt; "In operator=(MemoryBlock&amp;&amp;). length = "
                  &lt;&lt; other._length &lt;&lt; "." &lt;&lt; std::endl;

        swap(*this, other);
        return *this;
    }

    void swap(MemoryBlock&amp; first, MemoryBlock&amp; second)
    {
        using std::swap;
        swap(first._length, second._length);
        swap(first._data, second._data);
    }

private:
    size_t _length; // The length of the resource.
    int* _data; // The resource.
};

int main()
{
   // Create a vector object and add a few elements to it.
   vector&lt;MemoryBlock&gt; v;
   v.push_back(MemoryBlock(25));
   v.push_back(MemoryBlock(75));

   // Insert a new element into the second position of the vector.

   v.insert(v.begin() + 1, MemoryBlock(50));
}
</code></pre>

<p>Now when I run the code I have the following output:</p>

<pre><code>In MemoryBlock(size_t). length = 25.
In MemoryBlock(MemoryBlock&amp;&amp;). length = 25. Moving resource.
In operator=(MemoryBlock&amp;&amp;). length = 25.
In ~MemoryBlock(). length = 0.
In MemoryBlock(size_t). length = 75.
In MemoryBlock(MemoryBlock&amp;&amp;). length = 75. Moving resource.
In operator=(MemoryBlock&amp;&amp;). length = 75.
In MemoryBlock(const MemoryBlock&amp;). length = 25. Copying resource.
In ~MemoryBlock(). length = 25. Deleting resource.
In ~MemoryBlock(). length = 0.
In MemoryBlock(size_t). length = 50.
In MemoryBlock(MemoryBlock&amp;&amp;). length = 50. Moving resource.
In operator=(MemoryBlock&amp;&amp;). length = 50.
In MemoryBlock(const MemoryBlock&amp;). length = 25. Copying resource.
In MemoryBlock(const MemoryBlock&amp;). length = 75. Copying resource.
In ~MemoryBlock(). length = 25. Deleting resource.
In ~MemoryBlock(). length = 75. Deleting resource.
In ~MemoryBlock(). length = 0.
In ~MemoryBlock(). length = 25. Deleting resource.
In ~MemoryBlock(). length = 50. Deleting resource.
In ~MemoryBlock(). length = 75. Deleting resource.
</code></pre>

<p>What I don't understand why sometimes the copy constructor is called over the move constructor?</p>

<p>If I remove the move constructor definition, and declare it as default:</p>

<pre><code>// Move constructor.
MemoryBlock(MemoryBlock&amp;&amp; other) = default;
</code></pre>

<p>Then I get the right output:</p>

<pre><code>In MemoryBlock(size_t). length = 25.
In ~MemoryBlock(). length = 25. Deleting resource.
In MemoryBlock(size_t). length = 75.
In ~MemoryBlock(). length = 25. Deleting resource.
In ~MemoryBlock(). length = 75. Deleting resource.
In MemoryBlock(size_t). length = 50.
In ~MemoryBlock(). length = 25. Deleting resource.
In ~MemoryBlock(). length = 75. Deleting resource.
In ~MemoryBlock(). length = 50. Deleting resource.
In ~MemoryBlock(). length = 25. Deleting resource.
In ~MemoryBlock(). length = 50. Deleting resource.
In ~MemoryBlock(). length = 75. Deleting resource.
</code></pre>

<p>(the constructor calls are missing from output which means the move constructor was used)</p>