Once a lock on std::weak_ptr has returned nullptr, can it ever be non-null again?

is there every a condition under which it could return non-null, or is it effectively dead?

Well, it's "almost" dead at that point. lock calls expired in a way equivalent to this:

expired() ? shared_ptr<T>() : shared_ptr<T>(*this)

Then, looking at the expiration condition:

Equivalent to use_count() == 0. The destructor for the managed object may not yet have been called, but this object's destruction is imminent (or may have already happened).

Either way, there's no possibility for that specific weak_ptr to be reassigned to another value.


#include <iostream>
#include <memory>

int
main()
{
    using namespace std;
    shared_ptr<int> p(new int{3});
    weak_ptr<int> w = p;
    p = nullptr;
    auto l = w.lock();
    if (l == nullptr)
        cout << "l = nullptr\n";
    else
        cout << "l = " << *l << '\n';
    p.reset(new int{4});
    w = p;
    l = w.lock();
    if (l == nullptr)
        cout << "l = nullptr\n";
    else
        cout << "l = " << *l << '\n';
}

Portable output:

l = nullptr
l = 4

So yes, if the weak_ptr is assigned from a non-empty shared_ptr, it can become "reanimated from the dead."

Maybe that isn't what you meant. But it is what you asked.

Tags:

C++

C++11