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

What is printed by this program that uses a custom deleter and a weak_ptr?

#include <memory> #include <iostream> int main() { std::weak_ptr<int> w; { std::shared_ptr<int> s(new int(42), [](int* p){ std::cout << "del "; delete p; }); w = s; std::cout << w.expired() << " "; } std::cout << w.expired() << " "; auto locked = w.lock(); std::cout << (locked ? "yes" : "no"); }