title: invasive vs non-invasive ref-counted pointers in C++
body: <p>For the past few years, I've generally accepted that</p>
  
  <p>if I am going to use ref-counted smart pointers</p>
  
  <p>invasive smart pointers is the way to go</p>
  
  <p>--</p>
  
  <p>However, I'm starting to like non-invasive smart pointers due to the following:</p>
  
  <ol>
  <li>I only use smart pointers (so no Foo* lying around, only Ptr)</li>
  <li>I'm starting to build custom allocators for each class. (So Foo would overload operator new).</li>
  <li>Now, if Foo has a list of all Ptr (as it easily can with non-invasive smart pointers).</li>
  <li>Then, I can avoid memory fragmentation issues since class Foo move the objects around (and just update the corresponding Ptr).</li>
  </ol>
  
  <p>The only reason why this Foo moving objects around in non-invasive smart pointers being easier than invasive smart pointers is:</p>
  
  <p>In non-invasive smart pointers, there is only one pointer that points to each Foo.</p>
  
  <p>In invasive smart pointers, I have no idea how many objects point to each Foo.</p>
  
  <p>Now, the only cost of non-invasive smart pointers ... is the double indirection. [Perhaps this screws up the caches].</p>
  
  <p>Does anyone have a good study of expensive this extra layer of indirection is?</p>
  
  <p>EDIT: by smart pointers, I may be referring to what others call "shared-pointers"; the whole idea is: there is a reference-count attached to objects, and when it hits 0, the object is automatically deleted</p>