std::shared_ptr function to return a weak_ptr

1,060 views
Skip to first unread message

benk...@gmail.com

unread,
Nov 27, 2012, 12:19:11 AM11/27/12
to std-pr...@isocpp.org
Hi everyone,

I have a very simple proposal.  I find myself capturing weak pointers a lot in lambda expressions for signals or message queues between threads.  Something like this:
auto ptr(shared_from_this());
std
::weak_ptr< SomeComplexAndLongTypename > weakPtr(ptr);
signal
([weakPtr] ()
{
   
auto ptr = weakPtr.lock();
   
if (ptr)
   
{
       
...
   
}
});

I would like a member function in shared_ptr that returns a weak_ptr of the same type so that I can write:
auto weakPtr(ptr.weak());

The member function would be very simple:
std::weak_ptr< T > weak() const { return std::weak_ptr< T >(*this); }

(I know I can create a free function, but it still ends up being more typing.)
namespace myCode
{
   
template < typename T >
    std
::weak_ptr < T > weak(const std::shared_ptr< T >& ptr)
   
{
       
return std::weak_ptr< T >(ptr);
   
}
}

Does anyone else think this is worthwhile?  If I write a proposal, would the committee take it seriously?

Thanks,
Ben


Peter Sommerlad

unread,
Nov 27, 2012, 2:43:53 AM11/27/12
to std-pr...@isocpp.org
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.

Just my CHF0.02
Peter.
> --
>
>
>

--
Prof. Peter Sommerlad

Institut für Software: Bessere Software - Einfach, Schneller!
HSR Hochschule für Technik Rapperswil
Oberseestr 10, Postfach 1475, CH-8640 Rapperswil

http://ifs.hsr.ch http://cute-test.com http://linticator.com http://includator.com
tel:+41 55 222 49 84 == mobile:+41 79 432 23 32
fax:+41 55 222 46 29 == mailto:peter.s...@hsr.ch





Ville Voutilainen

unread,
Nov 27, 2012, 2:57:01 AM11/27/12
to std-pr...@isocpp.org
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. So, summa summarum,
I'd like to see the proposal.

Nicol Bolas

unread,
Nov 27, 2012, 4:02:40 AM11/27/12
to std-pr...@isocpp.org


On Monday, November 26, 2012 11:57:02 PM UTC-8, Ville Voutilainen wrote:
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.

That's fairly obvious: shared_ptr would ensure that the object is still around. weak_ptr does not. Thus, the lambda does not keep the object in existence, while weak_ptr provides a mechanism to test to see if it currently exists or not.

This is common for things like callbacks or other non-essential lambdas that it is OK for them to outlive the objects they reference.
 

Xeo

unread,
Nov 28, 2012, 9:46:06 PM11/28/12
to std-pr...@isocpp.org, benk...@gmail.com
template<class T>
std
::weak_ptr<T> to_weak(std::shared_ptr<T> const& p){ return {p}; }
// ...
auto weakPtr = to_weak(ptr);

Doesn't this suffice? :)
Reply all
Reply to author
Forward
0 new messages