So this is a side effect from those olden times at the beginning of C++, when the standard makers decided to have two different delete
and delete[]
operators for pointers to objects and pointers to arrays of objects.
In these modern times of C++, where we have templates (they weren't there from the beginning), std::array
(for fixed sized arrays), inititalizer lists (for static fixed sized arrays) and std::vector
(for dynamically sized arrays), almost nobody will need the delete[]
operator anymore. I have never used it, and I wouldn't be surprised, if the vast majority of the readers of this question have not used it, either.
Removing int* array = new int[5];
in favour of auto* array = new std::array<int, 5>;
would simplify things and would enable safe conversion of pointers to std::unique_ptr
and std::shared_ptr
. But it would break old code, and so far, the C++ standard maintainers have been very keen on backwards compatibility.
Nobody stops you, though, from writing a small inlined templated wrapper function:
template<typename T>
std::unique_ptr<T> unique_obj_ptr(T* object) {
static_assert(!std::is_pointer<T>::value, "Cannot use pointers to pointers here");
return std::unique_ptr<T>(object);
}
Of course, you can also create a similiar function shared_obj_ptr()
to create std::shared_ptr
s, and if you really need them, you can also add unique_arr_ptr()
and shared_arr_ptr()
.