Boost logo

Boost Users :

From: ajb_at_[hidden]
Date: 2004-12-09 20:20:50


G'day.

Quoting Niklas Wiberg <nwlists-boostusers_at_[hidden]>:

> Basket has a function that is merely a query (hasApple) and involves no
> ownership-changes.
[...]
> On one hand, I can see how using a weak_ptr as the parameter type would
> signal to users that hasApple has no intention of modifying ownership of
> the pointed-to object.

No, because weak_ptrs can be turned into shared_ptrs.

What you want in this case is an unmanaged pointer. That is, you want
a raw, dumb, C-style pointer.

Think about it for a moment. If hasApple() truly does not manage the
lifetime of the Apple which it is passed, then presumably its lifetime
is already being managed by the caller (or someone further up the call
chain). Hence, it will exist for the duration of the call to hasApple(),
so a raw pointer is safe.

It's also better documentation. Use an unmanaged pointer for something
that doesn't need to be managed.

BTW, as a rule of thumb, passing a weak_ptr is almost always the wrong
thing to do. The biggest exception, of course, is passing a weak_ptr
by non-const reference expecting the callee to fill it in. There are
other exceptions, but IME they're quite rare.

Cheers,
Andrew Bromage


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net