On Tue, Dec 23, 2014 at 8:48 AM, L. David Baron <dba...@dbaron.org> wrote:

> On Tuesday 2014-12-23 08:36 -0800, Eric Rescorla wrote:
> > > Why not pass the raw pointer to the function?
> >
> >
> > My general theory is that smart pointers, once boxed, should never be
> > unboxed.
> > The major arguments I see for unboxing is performance, but if you pass a
> > ref,
> > then you don't have the increment/decrement cycle.
> >
> >
> >
> > >   That's what we do in most of the places in the code.
> > >
> >
> > Yes, I think this is unwise.
>
> Our convention has always been to pass raw pointers, generally with
> the assumption that the caller is expected to ensure the pointer
> lives across the function call.
>
> Passing references to smart pointers doesn't work if you need
> implicit conversions from derived class to base class (e.g., if the
> function takes Base*, and the caller has nsRefPtr<Derived>), which
> is pretty important in many areas of our codebase.
>

Hmm... This seems to work fine with RefPtr, but it *doesn't* work
with nsRefPtr (see end of message for the test code). I'm guessing
because of:

http://dxr.mozilla.org/mozilla-central/source/mfbt/RefPtr.h?from=RefPtr&case=true#239


Passing smart pointers (non-references) would cause tons of extra
> reference count traffic, which is particularly problematic when the
> reference-counting functions are virtual and/or threadsafe.


Right, that's the reason for using references.

-Ekr



class Base
{
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Base);

 private:
  ~Base() {}
};

class Derived : public Base {
};


void base_ref(const RefPtr<Base>& b) {
}

void base_ptr(Base* b) {
}

void foo() {
  RefPtr<Base> base;
  RefPtr<Derived> derived;

  base_ref(base);
  base_ref(derived);

  base_ptr(base);
  base_ptr(derived)
}
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to