I was wondering why dynamic arrays are directly supported by std::unique_ptr<>
but not by std::shared_ptr<>
:
unique_ptr<int[]> ptr1(new int[n]); /// OK!
shared_ptr<int[]> ptr2(new int[n]); /// Incorrect: will not call delete[]
Update: I found out that the second line can be rewritten as:
shared_ptr<int> ptr2(new int[n], default_delete<int[]>());
Now I am wondering what's going on behind the scene that makes std::shared_ptr
works with the second approach and not in the way similar to std::unique_ptr
?