std::unique_ptr defines only operator[] in the specialization for arrays: std::unique_ptr<T[]> . For pointers without an array, the [] operator makes no sense (only [0] ).
There is no such specialization for std::shared_ptr (in C ++ 11), which is discussed in the corresponding question: Why does std :: shared_ptr <T []> not exist?
You should not use a smart pointer without an array with array distribution, unless you provide a custom deaerator. In particular, unique_ptr<int> p = new int[10] bad, since it calls delete instead of delete[] . Instead, use unique_ptr<int[]> , which calls delete[] . (And this one implements operator[] ). If you use shared_ptr to store T[] , you need to use a custom delector. See Also shared_ptr in an array: should I use it? - but it does not provide operator[] , since it uses type erasure to distinguish between an array and a non-array (the type of smart pointer does not depend on the provided deleter).
If you are wondering why there is no specialization of shared_ptr for arrays: it was a proposal, but it was not included in the standard (mainly with , you can get by writing ptr.get() + i for ptr[i] ).
source share