3

I am trying to learn the new features in C++11. And I am testing the following code in XCode.

#include <iostream>
#include <string>
#include <vector>

class CClass
{
    std::string s;
public:
    CClass()
    {
        std::cout<<"Default Constructor"<<std::endl;
    }
    CClass(const std::string v) :s(v) {
        std::cout<<"Constructor"<<std::endl;
    }

    CClass(const CClass& other): s(other.s) {
        std::cout<<"Copy Constructor"<<std::endl;
    }
    CClass(CClass&& a) noexcept
    {
        std::cout<<"Move Constructor"<<std::endl;
        s = std::move(a.s);
    }
    CClass& operator = (const CClass& other)noexcept
    {
        std::cout<<"Copy Assignment"<<std::endl;
        if(this != &other)
        {
            s = other.s;
        }
        return *this;
    }
    CClass& operator = (CClass&& other) noexcept
    {
        std::cout<<"Move Assignment"<<std::endl;
        if(this != &other)
        {
            s = std::move(other.s);
        }
        return *this;
    }
};

int main()
{
    std::vector<CClass> v;
    CClass x("hello");
    //v.push_back(x);
    std::cout<<"--------------------"<<std::endl;
    v.emplace_back("uiuiu");
    std::cout<<"--------------------"<<std::endl;
}

When I uncomment the push back I get the following result:

Constructor
Copy Constructor
--------------------
Constructor
Move Constructor
--------------------

Otherwise, if I comment it, I get:

Constructor
--------------------
Constructor
--------------------

My question is why is the move constructor not being called in second case? It is only being called in the first case when vector is initially not empty.

4

1 回答 1

7

这是因为向量中的一个元素需要移动到新的内存位置。发生这种情况的原因是新大小会超过向量容量,因此必须为向量分配具有新容量的新内存。

来自std::vector::emplace_back

如果 newsize()大于capacity()则所有迭代器和引用(包括过去的迭代器)都无效。否则只有过去的迭代器无效。

迭代器和引用无效的原因相同:因为元素现在存储在内存中的新位置。

如果你reserve在第一种情况下调用,你会看到没有调用移动构造函数:

CClass x{"hello"}; // constructor
v.reserve(2); // make space for 2 elements (you could have also used resize)
v.push_back(x); // copy constructor
v.emplace_back("uiuiu"); // constructor
于 2017-07-03T17:30:22.477 回答