5

Why does std::shared_ptr accept a deleter as a constructor argument and not as a template parameter? Storing a type erased deleter seems to go against the C++ philosophy of not adding any unnecessary overhead to the standard library components.

The only logic I can see behind this is that since the cost of atomic increments of the reference count are so high, the cost of indirection through type erasure is ignored. But still it seems like unnecessary overhead.

Curious
  • 20,870
  • 8
  • 61
  • 146
  • 4
    "Effective Modern C++" states that in this way the deleter is not Part of the type, thus you can store for example to shared_ptr with different Deleters in the same Container. Its more flexible. – ab.o2c Jan 05 '17 at 07:14
  • @ab.o2c that does make sense, thanks – Curious Jan 05 '17 at 07:18
  • https://isocpp.org/blog/2015/01/deleteres - Which links to multiple SO questions actually. – Holt Jan 05 '17 at 07:18
  • A `shared_ptr` has to allocate the reference count separately anyway, so also storing the deleter in this extra memory block has a very low overhead. – Bo Persson Jan 05 '17 at 08:31

0 Answers0