Boost logo

Boost :

From: Ulrich Eckhardt (doomster_at_[hidden])
Date: 2006-04-06 01:50:47


http://boost.org/libs/smart_ptr/intrusive_ptr.html says:
"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."

I wonder why? Embedding the refcounter into the object seems to me much more
natural and performant than adding another free-store allocation which
shared_ptr requires.

Another related thing is why is there no complementary class to add a
refcounter to an object? Something like

template<typename ObjectType>
struct refcount
{
  refcount(): refs(0) {}
  ~refcount(){}
private:
  refcount( refcount const&);
  refcount& operator=(refcount const&);

  friend void intrusive_ptr_add_ref( ObjectType* o) {
    static_cast<refcount<ObjectType>*>(o)->add_ref();
  }
  void add_ref() {
    inc(refs);
  }
  friend void intrusive_ptr_release( ObjectType* o) {
    static_cast<refcount<ObjectType>*>(o)->release();
  }
  void release() {
    dec(refs);
    if(!refs)
      delete static_cast<ObjectType*>(this);
  }
  some-integer-type refs;
};

(I think that I can't legally inject functions into the surrounding namespace
via these friend functions, but maybe there's a workaround and this is just a
sketch. )

Uli


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk