unique_ptr in structure inside vector doesn't compile when resize used on vector

c++, c++11, unique-ptr, vector, visual-c++-2010

Solution

I hate to interrupt this conversation you're having with yourself. :-)

But the answer is that VS2010 does not yet fully implement the C++11 spec (which would require a bit of time travel). `Test` should have a defaulted noexcept move constructor that calls the `unique_ptr` move constructor. VS2010 does not implement this implicit `Test` move constructor. If it did, your complete example would compile and run as expected. `vector` will use noexcept move constructors to move things around when it needs to.

Problem

VS2010 I have a structure that has a `unique_ptr` inside. I then have a `vector` of this structure. If I try to resize (or use reserve on) the vector, I get compilation errors. Below is a stripped-down example that still exhibits the problem. ``` 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); } ``` The errors I get are complaints about accessing private members of `unique_ptr` (the assignment operators). The compiler is trying to dynamically construct `Test &Test::operator =(const Test &)` and `Test::Test(const Test &)`. I don't see why a resize operation would need to call either of these functions (why doesn't it just call the default constructor if it needs to?). Both of them present problems because it isn't possible to make either with `unique_ptr` due to the `const`.

Original source