Why does unique_ptr have the deleter as a type parameter while shared_ptr doesn't?

前端 未结 2 1545
一整个雨季
一整个雨季 2021-02-01 00:12

The std::unique_ptr template has two parameters: the type of the pointee, and the type of the deleter. This second parameter has a default value, so you usually just write somet

2条回答
  •  一个人的身影
    2021-02-01 00:51

    Another reason, in addition to the one pointed out by DeadMG, would be that it's possible to write

    std::unique_ptr a(new int[100]);
    

    and ~unique_ptr will call the correct version of delete (via default_delete<_Tp[]>) thanks to specializing for both T and T[].

提交回复
热议问题