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