7
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 shared_ptr signature is different for arrays when compared to unique_ptr?

It would have been simpler if both api followed similar signature.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
shivakumar
  • 3,297
  • 19
  • 28

1 Answers1

6

unique_ptr has an explicit template specialization to handle array objects. The LWG (Library Working Group of the C++ committee) briefly considered the possibility of shared_ptr<T[]> but apparently it wasn't of sufficiently high priority to make it into the standard, there was also some controversy over the consequences of the arithmetic on shared_ptr<T[]>

Community
  • 1
  • 1
Gearoid Murphy
  • 11,834
  • 17
  • 68
  • 86