Reading : The C++ Standard Library - A Tutorial and Reference
I came across with following piece :
Note that the default deleter provided by shared_ptr calls delete, not delete[]. This means that the default deleter is appropriate only if a shared pointer owns a single object created with new. Unfortunately, creating a shared_ptr for an array is possible but wrong:
std::shared_ptr<int> p(new int[10]); // ERROR, but compiles
So, if you use new[] to create an array of objects you have to define your own deleter. You can do that by passing a function, function object, or lambda, which calls delete[ ] for the passed ordinary pointer. For example:
std::shared_ptr<int> p(new int[10],
[](int* p) {
delete[] p;
});
Is there any specific reason behind this limitation? If shared pointer is smart, can't it be that smart that it keeps the info whether it's array or single object?
Though there is a way to do this :
std::shared_ptr<int> p(new int[10], std::default_delete<int[]>());