Unique_ptr in the structure inside the vector does not compile when resizing the vector

VS2010

I have a structure with unique_ptrinside. Then I have vectorthis structure. If I try to resize (or use the reserve) to a vector, I get compilation errors. Below is a stripped-down example that still detects 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);
}

I receive complaints about access to private members unique_ptr(assignment operators). The compiler is trying to dynamically build Test &Test::operator =(const Test &)and Test::Test(const Test &). I don’t understand why the resize operation should call any of these functions (why not call the default constructor if necessary?). Both of them present problems because it is impossible to do with help unique_ptrbecause of const.

+3
2

.: -)

, VS2010 ++ 11 ( ). Test noexcept move, unique_ptr. VS2010 Test. , , . vector noexcept move constructors , .

+6

( , )

, , , unique_ptr<> . , , resize(). , resize() . . , , - unique_ptr<> .

, unique_ptr<> , vector<unique_ptr<>> . , .

, , vector<Test>, , , . , shared_ptr<> , .

0

All Articles