How do you access a vector of unique_ptr

Viewed 2751

I am trying to get the hang of smart pointers. I have a vector of unique_ptr to double array which I am able to create, but can't access it.

vector<unique_ptr<double[]>> arrDoubles;

//...

unique_ptr<double[]>dataArr(new double[size]);
arrDoubles.push_back(move(dataArr));

Now,

auto datAr = arrDoubles[0];

gives the following :

error: use of deleted function ‘std::unique_ptr<_Tp [], _Dp>::unique_ptr(const std::unique_ptr<_Tp [], _Dp>&) [with _Tp = double; _Dp = std::default_delete<double []>]’
1 Answers

You are trying to create a copy of a unique_ptr which can't be done. Use a reference.

auto& datAr = arrDoubles[0];
Related