【发布时间】:2012-04-25 11:23:15
【问题描述】:
VS2010
我有一个内部有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。
【问题讨论】:
标签: c++ vector c++11 visual-c++-2010 unique-ptr