#include // for EXIT_SUCCESS #include // for std::cout, std::endl #include // for std::shared_ptr, std::weak_ptr using std::shared_ptr; using std::weak_ptr; using std::cout; using std::endl; int main(int argc, char** argv) { std::weak_ptr w; { // temporary inner scope std::shared_ptr y(new int(10)); w = y; // assignment of weak_ptr takes a shared_ptr std::shared_ptr x = w.lock(); // "promoted" shared_ptr std::cout << *x << " " << w.expired() << std::endl; } std::cout << w.expired() << std::endl; w.lock(); // returns a nullptr return EXIT_SUCCESS; }