Background
If I had a foo function as follows
void foo(std::vector<int>& values) { values = std::vector<int>(10, 1); }
Then I could call him
std::vector<int> values; foo(values);
Note that the initial vector empty, then it is filled inside the foo function.
I often come across interfaces that I cannot change (i.e. third-party) that have the same intentions as above, but using raw arrays like
void foo(int*& values) { values = new int[10]; std::fill_n(values, 10, 1); }
My problem is that now I am responsible for managing this memory, for example
int* values; foo(values); delete[] values;
Question
Is there a way to use smart pointers to manage this memory for me? I would like to do something like
std::unique_ptr<int[]> values; foo(values.get());
but get returns a pointer that is the value of r, so I cannot pass it using a link other than const.
source share