Why is the signature of shared_ptr different from unique_ptr for arrays?

std::unique_ptr<int[]> p(new int[10]); //ok std::shared_ptr<int[]> p(new int[10]); //Error shared_ptr<int> sp( new int[10],[](int *p){delete [] p;}); //Ok, writing custom deleter for //array since shared_ptr will call //delete by default. 

Is there any specific reason why the shared_ptr signature is different for arrays compared to unique_ptr?

It would be easier if both api followed a similar signature.

+6
source share
1 answer

unique_ptr has an explicit specialization pattern for processing array objects. The LWG (C ++ Committee Library Working Group) briefly considered the possibility of shared_ptr<T[]> , but it seems that it did not have a high enough priority to turn it into a standard, there were also some debates about the implications of arithmetic on shared_ptr<T[]>

+6
source

Source: https://habr.com/ru/post/955335/


All Articles