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
?
std::make_shared
supports arrays from C++20 en.cppreference.com/w/cpp/memory/shared_ptr/make_shared Why not earlier, I suppose because someone has to write the proposal, advocate for it, get 1 or more implementations written etc. They probably had something else scheduled. – Bevin