weak_ptr::expired



#include <memory>
#include <iostream>
                                                                                                         
using std::weak_ptr,
    std::shared_ptr,
    std::make_shared,
    std::boolalpha,
    std::cerr;

class Temp{
    public:
        int a, b;
        Temp(int i = 1, int j = 1):a(i), b(j){}
};

int main(){
    cerr << boolalpha;
    shared_ptr<Temp> p1 = make_shared<Temp>();
    weak_ptr<Temp> wp1(p1);

    //why would anybody do this?
    //the check lambda function wouldn't work
    {//auto check = [wp1](weak_ptr<Temp> w)
        shared_ptr<Temp> p2 = make_shared<Temp>();
        weak_ptr<Temp> wp2(p2);
        wp1 = wp2;
    }

//    check(wp1);
    cerr << "wp1 expired: " << wp1.expired() << "\n";
    return 0;
}      
C++ Examples© 2024 TBD