[Followup to this question]
I\'ve been dealing a little bit with smart pointers to c-style arrays recently. I ultimately wound up doing the recommended thing and usi
Yes, your example is valid for the very reasons you've stated. unique_ptr::pointer
is int *
, and you're trying to pass ownership of that to a shared_ptr<int>
, so the converting constructor you've listed will participate in overload resolution, and will make a copy of the deleter (std::default_delete<int[]>
) because it's not a reference type.
So the following is valid, and will call delete[]
when the shared_ptr
reference count goes to zero
std::shared_ptr<T> mySharedArray = std::make_unique<T[]>(16);
Another way to write this, other than the lambda you've shown, is
std::shared_ptr<T> mySharedArray(new T[16], std::default_delete<int[]>());
which will result in mySharedArray
acquiring the same deleter as the previous line.