VS2010的unique_ptr在內部向量結構當矢量
用於調整大小我有具有unique_ptr
內的結構不編譯。然後我有這個結構的vector
。如果我嘗試調整矢量大小(或使用保留),則會出現編譯錯誤。下面是一個精簡的例子,它仍然存在這個問題。
struct Test
{
unique_ptr<int> pValue;
};
void test()
{
// this doesn't compile
vector<Test> testVector;
testVector.resize(5);
// this also doesn't compile
testVector.reserve(5);
// this, of course, compiles
vector<unique_ptr<int>> testVector2;
testVector2.resize(5);
testVector2.reserve(5);
}
我得到的錯誤是關於訪問的unique_ptr
(賦值運算符)的私有成員的投訴。編譯器試圖動態構造Test &Test::operator =(const Test &)
和Test::Test(const Test &)
。我不明白爲什麼調整大小的操作需要調用這些函數(爲什麼它只是在需要時調用默認構造函數?)。他們都提出了問題,因爲由於const
而導致無法使用unique_ptr
。
我很欣賞中斷! :-)我從來不會猜測問題是V10(現在是VC11)不執行'noexcept'。 http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx – dsmtoday 2012-04-15 04:12:13