0

This post explains what an intrusive pointer in boost is. As far as I can see, an intrusive pointer is essentially the same as a shared pointer except the user must provide an interface for the reference counter by defining intrusive_ptr_add_ref and intrusive_ptr_release functions.

Why would I want to manage reference count myself? What is the benefit of such approach?

Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
  • My guess : to avoid having double reference counts if the object pointed to already has refcounting. (https://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/intrusive_ptr.html). So it is a special case for optimization purposes, it even states : when in doubt just use std::shared_ptr – Pepijn Kramer Oct 10 '22 at 11:32
  • 3
    The boost manual mentions several [right in the intro](https://www.boost.org/doc/libs/1_80_0/libs/smart_ptr/doc/html/smart_ptr.html#intrusive_ptr). – n. m. could be an AI Oct 10 '22 at 11:36
  • 1
    @n.1.8e9-where's-my-sharem. and follows that with the advice "As a general rule, if it isn’t obvious whether `intrusive_ptr` better fits your needs than `shared_ptr`, try a `shared_ptr`-based design first." – Caleth Oct 10 '22 at 11:42
  • The boost documentation has a more detailed answer than should be expected from SO community – sehe Oct 10 '22 at 11:47

0 Answers0