Reputation: 175
as of C++17 you can use make_unique
in order to create smart pointers to arrays, such as:
unique_ptr<int[]> ptr = make_unique<int[]>(10);
which will create a smart pointer to an array of 10 elements (the fact that proper deleter[] will be called is also great).
However according to this make_shared
does not support such functionality (at least not in C++17, to my understanding):
shared_ptr<int[]> ptr = make_shared<int[]>(10);
the code above is apparently illegal. Indeed, my Visual Studio 2017 (v141) spits out the following error:
C2070: 'int[]': illegal sizeof operand'
What's interesting is that shared_ptr
itself does support array types (i.e., shared_ptr<int[]>
is legal), but make_shared
does not. Whereas make_unique
does.
The question is, what prevented the standard maker people to let make_shared
support array types, just like in the case of make_unique
?
Upvotes: 7
Views: 7457
Reputation: 38267
What prevented the standard maker people to let make_shared support array types [...]?
Probably nothing, this case was simply not considered, similar to std::make_unique
not being present in C++11 but added in C++14. And as pointed out in the comments, this missing piece will ship with C++20.
There is a difference between std::unique_ptr
and std::shared_ptr
that made neglecting raw arrays pointers easy, though: custom deleters are part of std::unique_ptr
's type but not part of std::shared_ptr
's type. Therefore, you can handle an array like this
std::shared_ptr<int> array = std::shared_ptr<int>(new int[10],
[](int *ptr){ delete []ptr; });
and hence delegate the correct memory cleanup to the point of object creation. This makes it easy to treat raw arrays as a special case of std::shared_ptr
instances.
Upvotes: 2