Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumC++shared_ptr and weak_ptrSingle-choice MCQ

Calling lock() on this weak_ptr returns what, and why?

#include <memory> #include <iostream> int main() { std::weak_ptr<int> w; { auto s = std::make_shared<int>(42); w = s; std::cout << w.expired() << ' '; } // s destroyed here auto locked = w.lock(); std::cout << w.expired() << ' ' << (locked == nullptr) << '\n'; }