Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-12-13 20:36:52

Theodore Witkamp wrote:

> If you notice it's my_weak_ptr not boost::weak_ptr. Which can be used
> in this way due to the implementation of get_pointer for that type.

This is dangerous since you may be left with a dangling this if something
invalidates the weak pointer while you're inside f, but you probably knew
that. (This is the reason why boost::weak_ptr can't be used in this way.)

The "no exceptions" constraint makes it harder than it needs to be. In this
situation I would probably use a null X object:

struct Xb
    virtual void f() = 0;

struct Xn: Xb
    void f() {} // stub

Xn nx;

struct X: Xb
    void f(); // do real work

Xb * lock( my_weak_ptr<Xb> px )
    return px? get_pointer( px ): &nx;

int main()
    my_shared_ptr<Xb> px( new X );
    my_weak_ptr<Xb> wp( px );

    function<void()> f = bind( &Xb::f, bind( lock, wp ) );




or something like that. Obviously untested. :-)

Boost list run by bdawes at, gregod at, cpdaniel at, john at