The standard provides a template specialization of std::unique_ptr
which correctly calls the delete[]
from its destructor:
void func()
{
std::unique_ptr< int[] > arr(new int[10]);
.......
}
With std::shared_ptr
this specialisation is not available, so it is necessary to
to provide a deleter which correctly calls delete[]
:
void func()
{
// Usage
shared_ptr array (new double [256], [](double* arr) { delete [] arr; } );
..............
}
Is this simply an oversight? (in the same way that there is an std::copy_if
) or is there a reason?
The LWG (Library Working Group of the C++ committee) briefly considered the possibility but the idea wasn't without controversy. Though the controversy was mainly about a feature added to the shared_ptr<T[]>
proposal that could have been jettisoned (arithmetic on shared_ptr<T[]>
).
But ultimately the real real reason is that though it was discussed, there was never an actual written proposal in front of the LWG to do this. It never bubbled up anyone's priority list (including my own) sufficiently to put the time into writing a proposal.
Informal conversations have recently begun anew on this topic among a few LWG members, and I have personally prototyped it. But there is still no written proposal for it. I think it would be a decent additional tool in the toolbox. Whether it will ever actually happen or not is anyone's guess.
Update
Array support for shared_ptr
now has a draft TS:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4077.html
Update (2017)
This is now supported in C++17. See case 3 of shared_ptr::shared_ptr()