#include <iostream>
#include <memory>

struct Trace {
    int value;
    explicit Trace(int initial) : value{initial} {}
    ~Trace() {
        std::cout << "destroy " << value << '\n';
    }
};

int main() {
    std::weak_ptr<Trace> watch{};
    {
        auto first = std::make_shared<Trace>(7);
        auto second = first;
        watch = first;
        std::cout << "owners " << first.use_count() << '\n';
        first.reset();
        std::cout << "remaining " << second->value << '\n';
        if (auto held = watch.lock(); held) {
            std::cout << "locked " << held->value << '\n';
        }
        second.reset();
    }
    std::cout << "expired " << watch.expired() << '\n';
    if (auto held = watch.lock(); held) {
        std::cout << "locked " << held->value << '\n';
    } else {
        std::cout << "empty\n";
    }
    return 0;
}
