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

前端 未结 2 1544
一整个雨季
一整个雨季 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 01:08

    Part of the reason is that shared_ptr needs an explicit control block anyway for the ref count and sticking a deleter in isn't that big a deal on top. unique_ptr however doesn't require any additional overhead, and adding it would be unpopular- it's supposed to be a zero-overhead class. unique_ptr is supposed to be static.

    You can always add your own type erasure on top if you want that behaviour- for example, you can have unique_ptr>, something that I have done in the past.

提交回复
热议问题