Quick Q: Is it useful to pass std::weak_ptr to functions?
▻http://isocpp.org/feeder/?FeederAction=clicked&feed=All+Posts&seed=http%3A%2F%2Fisocpp.org%2Fblog%2F2
Quick A: yes
Recently on SO:
Is it useful to pass std::weak_ptr to functions? Consider this toy example. struct PointerObserver
std::weak_ptr<int> held_pointer;
void observe( std::weak_ptr<int> p )
held_pointer = std::move(p);
void report() const
if ( auto sp = held_pointer.lock() )
std::cout << “Pointer points to” << *sp << “\n”;
else
std::cout << “Pointer has expired.\n”;
; In this example, a function observe holds state. Its weak_ptr parameter communicates that this smart pointer is not owning, but reserves the ability to own at a (...)
#News,Articles&_Books,