Smart pointers are library code, so they work the way they do, because someone created them that way.
In the first, new voice array, the second line does not make sense syntactically, since you cannot initialize the array with a pointer, and new returns a pointer.
The unique_ptr example unique_ptr also unique_ptr ; The fixed version makes more sense:
// +--------------+------------ pointer to int // VV std::unique_ptr<int> p { new int; } std::unique_ptr<int[]> p { new int[10]; } // ^ ^ // +----------------+---------- pointer to first element // of an array of int
Define a pattern?
The reason you need different specialized specializations is because you need to call either delete or delete[] in the pointer depending on how it was allocated, but you cannot say that you just looked at the raw pointer. (In addition, the array version provides a convenient [] operator.)
Nothing prevents you from mixing unique_ptr<int> and new int[10] , but it is a more or less subtle error that causes undefined to work quietly (another reason to never use new youreself and rely on make_unique !). On the contrary, your first example is a simple syntax error.
source share