我嘗試將類cBar的兩個實例放置到具有emplace_back函數的向量中。emplace_back調用移動構造函數和析構函數
根據reference調用emplace_back只保留向量中的位置,然後創建新的實例「in place」。現在
,我試着用它來試驗:
#include <vector>
#include <iostream>
#include <memory>
#include <string>
class cBar
{
public:
cBar(const int index);
cBar(cBar&& other); //needed for emplace_back?
~cBar();
private:
cBar(const cBar& other) = delete;
cBar& operator=(const cBar& other) = delete;
public:
int mIndex;
};
cBar::cBar(const int index) : mIndex(index)
{
std::cout << "cBar being created ..." << std::endl;
}
cBar::cBar(cBar&& other) : mIndex(other.mIndex)
{
std::cout << "cBar being moved ..." << std::endl;
}
cBar::~cBar()
{
std::cout << "cBar being destroyed ..." << std::endl;
}
int main()
{
std::vector<cBar> mBars;
std::cout << "Begin to create 2 cBar instance, I am expecting 2 \"cBar being created ...\" messages here" << std::endl;
mBars.emplace_back(0);//this supposed to call only cBar(const int index) constructor, and nothing more
mBars.emplace_back(1);//this works as expected, only one constructor call
//destroy all
std::cout << "Destroy the 2 isntances (this works, I see the two expected \"cBar being destroyed ...\" messages here" << std::endl;
mBars.clear();
std::cin.get();
return 0;
}
輸出:
Begin to create 2 cBar instance, I am expecting 2 "cBar being created ..." messages here
cBar being created ...
cBar being moved ...
cBar being destroyed ...
cBar being created ...
Destroy the 2 isntances (this works, I see the two expected "cBar being destroyed ..." messages here
cBar being destroyed ...
cBar being destroyed ...
如果你運行上面的一個,你會看到第一emplace_back「到位」創建實例,但是會立即調用移動構造函數,然後調用析構函數。
更奇怪的是,在第二個emplace的情況下,我看到了預期的行爲:只有一個構造函數調用。
我有兩個問題:
爲什麼我需要在我的類來定義移動構造函數,如果我只是想emplace_back項目,而從不使用的push_back。
在創建第一個實例的情況下,爲什麼移動構造函數,然後調用析構函數?如果我訪問第一個實例的數據似乎都正確,所以我不知道爲什麼移動構造函數和析構函數被調用。
我使用Visual Studio 2015年
輸出與向量的大小在每個步驟:
Begin to create 2 cBar instance, I am expecting 2 "cBar being created ..." messages here
Vector size:0
cBar being created ...
Vector size:1
cBar being moved ...
cBar being destroyed ...
cBar being created ...
Vector size:2
Destroy the 2 isntances (this works, I see the two expected "cBar being destroyed ..." messages here
cBar being destroyed ...
cBar being destroyed ...
在測試前添加'mBars.reserve(9);''。 –