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

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<T, std::function<void(T*)>>, something that I have done in the past.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)