Boost logo

Boost :

From: Joe Gottman (jgottman_at_[hidden])
Date: 2005-07-14 18:20:34


"Jaap Suter" <boost_at_[hidden]> wrote in message
news:003501c58833$f508a020$4119059a_at_unknown...
> Hi,
>
> a weak_ptr can be said to have three states.
>
> 1. Never been assigned to (default constructed)
> 2. Assigned to, lock will return a shared_ptr to a valid object.
> 3. Assigned to, but the object pointed to is dead and lock will return
> null.
>
> As far as I know it is currently impossible to distinguish between case 1
> and 3. Am I overlooking something? If not, can somebody explain the reason
> why it is not possible to distinguish the two cases? Admittedly, I cannot
> come up with any elegant design in which such a distinction is useful, so
> the question is merely theoretical.
>

   Actually, it is possible to distinguish case 1 from case 3:

   weak_ptr<Foo> wp = get_weak_ptr();
   if (wp.expired()) {
        weak_ptr emptyPtr; // Default constructed weak_ptr
        if (!(emptyPtr < wp) && !(wp < emptyPtr)) {
            cout << "Case 1" << endl
        } else {
            cout << "Case 3" << endl;
        }
    } else { // Not expired
        cout << "Case 2" << endl;
    }
}

It is vital that a weak_ptr that is put into a set and later expires does
not change its ordering relative to the other elements of the set.

Joe Gottman


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