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?
shared_ptr
machinery should be disabled when working with arrays, such as the ability to refer to a subobject. – Saltation