auto ptr(shared_from_this());
std::weak_ptr< SomeComplexAndLongTypename > weakPtr(ptr);
signal([weakPtr] ()
{
auto ptr = weakPtr.lock();
if (ptr)
{
...
}
});
auto weakPtr(ptr.weak());
std::weak_ptr< T > weak() const { return std::weak_ptr< T >(*this); }
namespace myCode
{
template < typename T >
std::weak_ptr < T > weak(const std::shared_ptr< T >& ptr)
{
return std::weak_ptr< T >(ptr);
}
}
On 27 November 2012 09:43, Peter Sommerlad <peter.s...@hsr.ch> wrote:
> Hi,
> There is a similar functionality in the "opposite direction" wrt to future and shared_future, so why not?
> should fit into shared_ptr observers section.
I would like to see the practical rationale _why_ you often capture
weak_ptrs. I'm not saying it's
not a valid use, I'm just curious why capture weak_ptrs rather than
shared_ptrs.
template<class T>
std::weak_ptr<T> to_weak(std::shared_ptr<T> const& p){ return {p}; }
// ...
auto weakPtr = to_weak(ptr);