Topic: std::shared_ptr function to return a weak_ptr


Author: benkerby@gmail.com
Date: Mon, 26 Nov 2012 21:19:11 -0800 (PST)
Raw View
------=_Part_136_5008478.1353993551475
Content-Type: text/plain; charset=ISO-8859-1

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


--




------=_Part_136_5008478.1353993551475
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi everyone,<br><br>I have a very simple proposal.&nbsp; I find myself capt=
uring weak pointers a lot in lambda expressions for signals or message queu=
es between threads.&nbsp; Something like this:<br><div class=3D"prettyprint=
" style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187=
, 187); border-style: solid; border-width: 1px; word-wrap: break-word;"><co=
de class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color=
: #008;" class=3D"styled-by-prettify">auto</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> ptr</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify">shared_from_this</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">());</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"><br>std</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify">weak_ptr</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">SomeComple=
xAndLongTypename</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt=
;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> weakPtr<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">ptr</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">);</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><br>signal</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">([</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">weakPtr</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">]</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">()</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &=
nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">auto=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> ptr </spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"> weakPtr</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">lock</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">();</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><span style=3D"color:=
 #008;" class=3D"styled-by-prettify">if</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify">ptr</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&=
nbsp; &nbsp; </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&n=
bsp; &nbsp; &nbsp; &nbsp; </span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">...</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br>&nbsp; &nbsp; </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">});</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><=
/span></div></code></div><br>I would like a member function in shared_ptr t=
hat returns a weak_ptr of the same type so that I can write:<br><div class=
=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-colo=
r: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wrap: b=
reak-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span=
 style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> weakPtr</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">ptr</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">weak</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">());</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br></span></div></code></div><br>The member function would be =
very simple:<br><div class=3D"prettyprint" style=3D"background-color: rgb(2=
50, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; borde=
r-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div clas=
s=3D"subprettyprint"><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify">weak_ptr</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> T </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> weak</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">()</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">const</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> std</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">weak_ptr</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> T </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">&gt;(*</span><span style=3D"color: #008;"=
 class=3D"styled-by-prettify">this</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r></span></div></code></div><br>(I know I can create a free function, but i=
t still ends up being more typing.)<br><div class=3D"prettyprint" style=3D"=
background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); bor=
der-style: solid; border-width: 1px; word-wrap: break-word;"><code class=3D=
"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">namespace</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> myCode<br></span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"><br>&nbsp; &nbsp; </span><span style=3D"color: #008;" cla=
ss=3D"styled-by-prettify">template</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">=
typename</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> T=
 </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbs=
p; std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify">weak_ptr </s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> T </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> weak</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">const</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> std</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify">shared_ptr</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> T </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt=
;&amp;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> ptr=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; <=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; &n=
bsp; &nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify=
">return</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> s=
td</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify">weak_ptr</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> T </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">&gt;(</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">ptr</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br></span></div></code></div><br>Does anyone else think this is w=
orthwhile?&nbsp; If I write a proposal, would the committee take it serious=
ly?<br><br>Thanks,<br>Ben<br><br><br>

<p></p>

-- <br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_136_5008478.1353993551475--

.


Author: Peter Sommerlad <peter.sommerlad@hsr.ch>
Date: Tue, 27 Nov 2012 08:43:53 +0100
Raw View
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.

On 27.11.2012, at 06:19, <benkerby@gmail.com> <benkerby@gmail.com> wrote:

> Hi everyone,
>=20
> I have a very simple proposal.  I find myself capturing weak pointers a l=
ot=20
> in lambda expressions for signals or message queues between threads. =20
> Something like this:
> auto ptr(shared_from_this());
> std::weak_ptr< SomeComplexAndLongTypename > weakPtr(ptr);
> signal([weakPtr] ()
> {
>    auto ptr =3D weakPtr.lock();
>    if (ptr)
>    {
>        ...
>    }
> });
>=20
> I would like a member function in shared_ptr that returns a weak_ptr of t=
he=20
> same type so that I can write:
> auto weakPtr(ptr.weak());
>=20
> The member function would be very simple:
> std::weak_ptr< T > weak() const { return std::weak_ptr< T >(*this); }
>=20
> (I know I can create a free function, but it still ends up being more=20
> typing.)
> namespace myCode
> {
>    template < typename T >
>    std::weak_ptr < T > weak(const std::shared_ptr< T >& ptr)
>    {
>        return std::weak_ptr< T >(ptr);
>    }
> }
>=20
> Does anyone else think this is worthwhile?  If I write a proposal, would=
=20
> the committee take it seriously?
>=20
> Thanks,
> Ben
>=20
>=20
> --=20
>=20
>=20
>=20

--=20
Prof. Peter Sommerlad

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

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





--=20




.


Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Tue, 27 Nov 2012 09:57:01 +0200
Raw View
On 27 November 2012 09:43, Peter Sommerlad <peter.sommerlad@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.

--




.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Tue, 27 Nov 2012 01:02:40 -0800 (PST)
Raw View
------=_Part_823_26240059.1354006960983
Content-Type: text/plain; charset=ISO-8859-1



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<javascript:>>
> 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.


> So, summa summarum,
> I'd like to see the proposal.
>

--




------=_Part_823_26240059.1354006960983
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<br><br>On Monday, November 26, 2012 11:57:02 PM UTC-8, Ville Voutilainen w=
rote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8e=
x;border-left: 1px #ccc solid;padding-left: 1ex;">On 27 November 2012 09:43=
, Peter Sommerlad &lt;<a href=3D"javascript:" target=3D"_blank" gdf-obfusca=
ted-mailto=3D"pixOdDidsKUJ">peter.s...@hsr.ch</a>&gt; wrote:
<br>&gt; Hi,
<br>&gt; There is a similar functionality in the "opposite direction" wrt t=
o future and shared_future, so why not?
<br>&gt; should fit into shared_ptr observers section.
<br>
<br>I would like to see the practical rationale _why_ you often capture
<br>weak_ptrs. I'm not saying it's
<br>not a valid use, I'm just curious why capture weak_ptrs rather than
<br>shared_ptrs.</blockquote><div><br>That's fairly obvious: shared_ptr wou=
ld ensure that the object is still around. weak_ptr does not. Thus, the lam=
bda does not keep the object in existence, while weak_ptr provides a mechan=
ism to test to see if it currently exists or not.<br><br>This is common for=
 things like callbacks or other non-essential lambdas that it is OK for the=
m to outlive the objects they reference.<br>&nbsp;</div><blockquote class=
=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #cc=
c solid;padding-left: 1ex;">So, summa summarum,
<br>I'd like to see the proposal.
<br></blockquote>

<p></p>

-- <br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_823_26240059.1354006960983--

.


Author: Xeo <hivemaster@hotmail.de>
Date: Wed, 28 Nov 2012 18:46:06 -0800 (PST)
Raw View
------=_Part_391_16199444.1354157166214
Content-Type: text/plain; charset=ISO-8859-1

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? :)

On Tuesday, November 27, 2012 6:19:11 AM UTC+1, benk...@gmail.com wrote:
>
> 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
>
>
>

--




------=_Part_391_16199444.1354157166214
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); b=
order-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; wo=
rd-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettypr=
int"><span style=3D"color: #008;" class=3D"styled-by-prettify">template</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">class</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> T</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"><br>std</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">weak_ptr</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&lt;</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">T</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> to_weak</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">std</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">shared_ptr</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">T</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">const</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">&amp;</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> p</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">){</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">p</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">};</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"><br></span><span style=3D"color: #800;" class=3D"styled-by-prettify"=
>// ...</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
></span><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> weakPtr </span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> to_weak</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">ptr</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br></span></div></code></div><br>Doesn't this su=
ffice? :)<br><br>On Tuesday, November 27, 2012 6:19:11 AM UTC+1, benk...@gm=
ail.com wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Hi everyone,<br>=
<br>I have a very simple proposal.&nbsp; I find myself capturing weak point=
ers a lot in lambda expressions for signals or message queues between threa=
ds.&nbsp; Something like this:<br><div style=3D"background-color:rgb(250,25=
0,250);border-color:rgb(187,187,187);border-style:solid;border-width:1px;wo=
rd-wrap:break-word"><code><div><span style=3D"color:#008">auto</span><span =
style=3D"color:#000"> ptr</span><span style=3D"color:#660">(</span><span st=
yle=3D"color:#000">shared_from_this</span><span style=3D"color:#660">());</=
span><span style=3D"color:#000"><br>std</span><span style=3D"color:#660">::=
</span><span style=3D"color:#000">weak_ptr</span><span style=3D"color:#660"=
>&lt;</span><span style=3D"color:#000"> </span><span style=3D"color:#606">S=
omeComplexAndLongTypename</span><span style=3D"color:#000"> </span><span st=
yle=3D"color:#660">&gt;</span><span style=3D"color:#000"> weakPtr</span><sp=
an style=3D"color:#660">(</span><span style=3D"color:#000">ptr</span><span =
style=3D"color:#660">);</span><span style=3D"color:#000"><br>signal</span><=
span style=3D"color:#660">([</span><span style=3D"color:#000">weakPtr</span=
><span style=3D"color:#660">]</span><span style=3D"color:#000"> </span><spa=
n style=3D"color:#660">()</span><span style=3D"color:#000"><br></span><span=
 style=3D"color:#660">{</span><span style=3D"color:#000"><br>&nbsp; &nbsp; =
</span><span style=3D"color:#008">auto</span><span style=3D"color:#000"> pt=
r </span><span style=3D"color:#660">=3D</span><span style=3D"color:#000"> w=
eakPtr</span><span style=3D"color:#660">.</span><span style=3D"color:#008">=
lock</span><span style=3D"color:#660">();</span><span style=3D"color:#000">=
<br>&nbsp; &nbsp; </span><span style=3D"color:#008">if</span><span style=3D=
"color:#000"> </span><span style=3D"color:#660">(</span><span style=3D"colo=
r:#000">ptr</span><span style=3D"color:#660">)</span><span style=3D"color:#=
000"><br>&nbsp; &nbsp; </span><span style=3D"color:#660">{</span><span styl=
e=3D"color:#000"><br>&nbsp; &nbsp; &nbsp; &nbsp; </span><span style=3D"colo=
r:#660">...</span><span style=3D"color:#000"><br>&nbsp; &nbsp; </span><span=
 style=3D"color:#660">}</span><span style=3D"color:#000"><br></span><span s=
tyle=3D"color:#660">});</span><span style=3D"color:#000"><br></span></div><=
/code></div><br>I would like a member function in shared_ptr that returns a=
 weak_ptr of the same type so that I can write:<br><div style=3D"background=
-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;bo=
rder-width:1px;word-wrap:break-word"><code><div><span style=3D"color:#008">=
auto</span><span style=3D"color:#000"> weakPtr</span><span style=3D"color:#=
660">(</span><span style=3D"color:#000">ptr</span><span style=3D"color:#660=
">.</span><span style=3D"color:#000">weak</span><span style=3D"color:#660">=
());</span><span style=3D"color:#000"><br></span></div></code></div><br>The=
 member function would be very simple:<br><div style=3D"background-color:rg=
b(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-widt=
h:1px;word-wrap:break-word"><code><div><span style=3D"color:#000">std</span=
><span style=3D"color:#660">::</span><span style=3D"color:#000">weak_ptr</s=
pan><span style=3D"color:#660">&lt;</span><span style=3D"color:#000"> T </s=
pan><span style=3D"color:#660">&gt;</span><span style=3D"color:#000"> weak<=
/span><span style=3D"color:#660">()</span><span style=3D"color:#000"> </spa=
n><span style=3D"color:#008">const</span><span style=3D"color:#000"> </span=
><span style=3D"color:#660">{</span><span style=3D"color:#000"> </span><spa=
n style=3D"color:#008">return</span><span style=3D"color:#000"> std</span><=
span style=3D"color:#660">::</span><span style=3D"color:#000">weak_ptr</spa=
n><span style=3D"color:#660">&lt;</span><span style=3D"color:#000"> T </spa=
n><span style=3D"color:#660">&gt;(*</span><span style=3D"color:#008">this</=
span><span style=3D"color:#660">);</span><span style=3D"color:#000"> </span=
><span style=3D"color:#660">}</span><span style=3D"color:#000"><br></span><=
/div></code></div><br>(I know I can create a free function, but it still en=
ds up being more typing.)<br><div style=3D"background-color:rgb(250,250,250=
);border-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wr=
ap:break-word"><code><div><span style=3D"color:#008">namespace</span><span =
style=3D"color:#000"> myCode<br></span><span style=3D"color:#660">{</span><=
span style=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=3D"color:#00=
8">template</span><span style=3D"color:#000"> </span><span style=3D"color:#=
660">&lt;</span><span style=3D"color:#000"> </span><span style=3D"color:#00=
8">typename</span><span style=3D"color:#000"> T </span><span style=3D"color=
:#660">&gt;</span><span style=3D"color:#000"><br>&nbsp; &nbsp; std</span><s=
pan style=3D"color:#660">::</span><span style=3D"color:#000">weak_ptr </spa=
n><span style=3D"color:#660">&lt;</span><span style=3D"color:#000"> T </spa=
n><span style=3D"color:#660">&gt;</span><span style=3D"color:#000"> weak</s=
pan><span style=3D"color:#660">(</span><span style=3D"color:#008">const</sp=
an><span style=3D"color:#000"> std</span><span style=3D"color:#660">::</spa=
n><span style=3D"color:#000">shared_ptr</span><span style=3D"color:#660">&l=
t;</span><span style=3D"color:#000"> T </span><span style=3D"color:#660">&g=
t;&amp;</span><span style=3D"color:#000"> ptr</span><span style=3D"color:#6=
60">)</span><span style=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=
=3D"color:#660">{</span><span style=3D"color:#000"><br>&nbsp; &nbsp; &nbsp;=
 &nbsp; </span><span style=3D"color:#008">return</span><span style=3D"color=
:#000"> std</span><span style=3D"color:#660">::</span><span style=3D"color:=
#000">weak_ptr</span><span style=3D"color:#660">&lt;</span><span style=3D"c=
olor:#000"> T </span><span style=3D"color:#660">&gt;(</span><span style=3D"=
color:#000">ptr</span><span style=3D"color:#660">);</span><span style=3D"co=
lor:#000"><br>&nbsp; &nbsp; </span><span style=3D"color:#660">}</span><span=
 style=3D"color:#000"><br></span><span style=3D"color:#660">}</span><span s=
tyle=3D"color:#000"><br></span></div></code></div><br>Does anyone else thin=
k this is worthwhile?&nbsp; If I write a proposal, would the committee take=
 it seriously?<br><br>Thanks,<br>Ben<br><br><br></blockquote>

<p></p>

-- <br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_391_16199444.1354157166214--

.