Topic: Add "weak_from_this" to std::enable_shared_from_this
Author: Jared Grubb <jared.grubb@gmail.com>
Date: Sat, 24 Aug 2013 13:57:24 -0700
Raw View
I'd like to propose to add a new "weak_from_this" member function to the st=
d::enable_shared_from_this class. This has two advantages:
1. Enables use of 'auto':
auto weak_handle =3D weak_from_this();
2. Minor efficiency bump over today's version:
std::weak_ptr<Foo> weak_handle =3D shared_from_this(); // downgrade=
of a temporary shared_ptr object
I think every implementation of std::enable_shared_from_this is just a wrap=
per of a weak_ptr anyway. Providing a method that copies the internal varia=
ble is more efficient than using shared_from_this, which lock()s the intern=
al weak_ptr, only to just immediately degrade back to a weak reference.
This is my first attempt at proposing a feature, so forgive my newbness :)
Jared
--=20
---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.
.
Author: Nicol Bolas <jmckesson@gmail.com>
Date: Sat, 24 Aug 2013 18:00:57 -0700 (PDT)
Raw View
------=_Part_35_29875766.1377392457226
Content-Type: text/plain; charset=ISO-8859-1
On Saturday, August 24, 2013 1:57:24 PM UTC-7, Jared Grubb wrote:
>
> I'd like to propose to add a new "weak_from_this" member function to the
> std::enable_shared_from_this class.
I would prefer something less limited: a `to_weak_ptr` function that takes
a shared_ptr<T> and returns an appropriate weak_ptr<T>.
> This has two advantages:
>
> 1. Enables use of 'auto':
> auto weak_handle = weak_from_this();
>
auto weak_handle = to_weak_ptr(shared_from_this());
2. Minor efficiency bump over today's version:
> std::weak_ptr<Foo> weak_handle = shared_from_this(); // downgrade
> of a temporary shared_ptr object
I think every implementation of std::enable_shared_from_this is just a
> wrapper of a weak_ptr anyway.
But it doesn't *have* to be. And we shouldn't make features based on the
assumption that something will be implemented in a certain way.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_35_29875766.1377392457226
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Saturday, August 24, 2013 1:57:24 PM UTC-7, Jared Grubb=
wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.=
8ex;border-left: 1px #ccc solid;padding-left: 1ex;">I'd like to propose to =
add a new "weak_from_this" member function to the std::enable_shared_from_t=
his class.</blockquote><div><br>I would prefer something less limited: a `t=
o_weak_ptr` function that takes a shared_ptr<T> and returns an approp=
riate weak_ptr<T>.<br> </div><blockquote class=3D"gmail_quote" s=
tyle=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-le=
ft: 1ex;">This has two advantages:
<br>
<br>1. Enables use of 'auto':
<br> auto weak_handle =3D weak_from_this();<br></=
blockquote><div><br><div class=3D"prettyprint" style=3D"background-color: r=
gb(250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; b=
order-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div =
class=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">auto</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> weak_handle </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> t=
o_weak_ptr</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">shared_fr=
om_this</span><span style=3D"color: #660;" class=3D"styled-by-prettify">())=
;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></spa=
n></div></code></div><br></div><blockquote class=3D"gmail_quote" style=3D"m=
argin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"=
>
2. Minor efficiency bump over today's version:
<br> std::weak_ptr<Foo> weak_handle =3D sha=
red_from_this(); // downgrade of a temporary shared_ptr object <=
/blockquote><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-lef=
t: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
I think every implementation of std::enable_shared_from_this is just a wrap=
per of a weak_ptr anyway.</blockquote><div><br>But it doesn't <i>have</i> t=
o be. And we shouldn't make features based on the assumption that something=
will be implemented in a certain way.</div><br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_35_29875766.1377392457226--
.
Author: Jared Grubb <jared.grubb@gmail.com>
Date: Sat, 24 Aug 2013 18:47:53 -0700 (PDT)
Raw View
------=_Part_787_733241.1377395273666
Content-Type: text/plain; charset=ISO-8859-1
On Saturday, August 24, 2013 6:00:57 PM UTC-7, Nicol Bolas wrote:
>
> On Saturday, August 24, 2013 1:57:24 PM UTC-7, Jared Grubb wrote:
>>
>> I'd like to propose to add a new "weak_from_this" member function to the
>> std::enable_shared_from_this class.
>
>
> I would prefer something less limited: a `to_weak_ptr` function that takes
> a shared_ptr<T> and returns an appropriate weak_ptr<T>.
>
I agree this is also a desirable feature. I was converting a couple of my
libraries from boost::shared_ptr to std::shared_ptr, and as a transition, I
was adding a bunch of "auto" in places, but got stuck in the weak_ptr
cases, because I had to use the right weak_ptr variety, and there was no
built-in "trait" I could consult to get the corresponding weak_ptr variety.
> This has two advantages:
>>
>> 1. Enables use of 'auto':
>> auto weak_handle = weak_from_this();
>>
>
> auto weak_handle = to_weak_ptr(shared_from_this());
>
> 2. Minor efficiency bump over today's version:
>> std::weak_ptr<Foo> weak_handle = shared_from_this(); // downgrade
>> of a temporary shared_ptr object
>
> I think every implementation of std::enable_shared_from_this is just a
>> wrapper of a weak_ptr anyway.
>
>
> But it doesn't *have* to be. And we shouldn't make features based on the
> assumption that something will be implemented in a certain way.
>
I argue the feature is helpful no matter how it is implemented (although,
honestly, I cant really imagine any other way to do it that wouldnt involve
effectively reinventing a weak_ptr).
In C++11, constructing a weak_ptr via "shared_from_this" will create a +1
strong reference, then a +1 weak reference, then a -1 strong reference in
the ~shared_ptr(); so you require 3 reference count modifications.
If enable_shared_from_this uses a weak_ptr internally, then a
"weak_from_this" is just a +1 weak reference with no calls to lock().
If enable_shared_from_this uses some other implementation, it could (at
worst) implement weak_from_this using the naive "return
shared_from_this();" version that achieves the same as C++11 does today;
but it also could choose to do something more efficient.
So in all cases, you do no worse. But in most implementations, the
implementation is a one-liner call to a copy constructor, you get 'auto'
for free, and you gain a significant efficiency bump.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_787_733241.1377395273666
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><br>On Saturday, August 24, 2013 6:00:57 PM UTC-7, Nic=
ol Bolas wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-=
left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr=
">On Saturday, August 24, 2013 1:57:24 PM UTC-7, Jared Grubb wrote:<blockqu=
ote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1=
px #ccc solid;padding-left:1ex">I'd like to propose to add a new "weak_from=
_this" member function to the std::enable_shared_from_this class.</blockquo=
te><div><br>I would prefer something less limited: a `to_weak_ptr` function=
that takes a shared_ptr<T> and returns an appropriate weak_ptr<T&=
gt;.<br></div></div></blockquote><div><br></div><div>I agree this is also a=
desirable feature. I was converting a couple of my libraries from boost::s=
hared_ptr to std::shared_ptr, and as a transition, I was adding a bunch of =
"auto" in places, but got stuck in the weak_ptr cases, because I had to use=
the right weak_ptr variety, and there was no built-in "trait" I could cons=
ult to get the corresponding weak_ptr variety.</div><div> <span style=
=3D"font-size: 13px;"> </span></div><blockquote class=3D"gmail_quote" =
style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-l=
eft: 1ex;"><div dir=3D"ltr"><blockquote class=3D"gmail_quote" style=3D"marg=
in:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex">This ha=
s two advantages:
<br>
<br>1. Enables use of 'auto':
<br> auto weak_handle =3D weak_from_this();<br></=
blockquote><div><br><div style=3D"background-color:rgb(250,250,250);border-=
color:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-=
word"><code><div><span style=3D"color:#008">auto</span><span style=3D"color=
:#000"> weak_handle </span><span style=3D"color:#660">=3D</span><span style=
=3D"color:#000"> to_weak_ptr</span><span style=3D"color:#660">(</span><span=
style=3D"color:#000">shared_from_this</span><span style=3D"color:#660">()<=
wbr>);</span><span style=3D"color:#000"><br></span></div></code></div><br><=
/div><blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;=
border-left:1px #ccc solid;padding-left:1ex">
2. Minor efficiency bump over today's version:
<br> std::weak_ptr<Foo> weak_handle =3D sha=
red_from_this(); // downgrade of a temporary shared_ptr object <=
/blockquote><blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left=
:0.8ex;border-left:1px #ccc solid;padding-left:1ex">
I think every implementation of std::enable_shared_from_this is just a wrap=
per of a weak_ptr anyway.</blockquote><div><br>But it doesn't <i>have</i> t=
o be. And we shouldn't make features based on the assumption that something=
will be implemented in a certain way.</div></div></blockquote><div><br></d=
iv><div>I argue the feature is helpful no matter how it is implemented (<sp=
an style=3D"font-size: 13px;">although, honestly, I cant really imagine any=
other way to do it that wouldnt involve effectively reinventing a weak_ptr=
)</span><span style=3D"font-size: 13px;">.</span></div><div><span style=3D"=
font-size: 13px;"><br></span></div><div><span style=3D"font-size: 13px;">In=
C++11, constructing a weak_ptr via "shared_from_this" will create a +1 str=
ong reference, then a +1 weak reference, then a -1 strong reference in the =
~shared_ptr(); so you require 3 reference count modifications.</span></div>=
<div><br></div><div>If enable_shared_from_this uses a weak_ptr internally, =
then a "weak_from_this" is just a +1 weak reference with no calls to lock()=
..</div><div><br></div><div>If enable_shared_from_this uses some other imple=
mentation, it could (at worst) implement weak_from_this using the naive "re=
turn shared_from_this();" version that achieves the same as C++11 does toda=
y; but it also could choose to do something more efficient.</div><div><br><=
/div><div>So in all cases, you do no worse. But in most implementations, th=
e implementation is a one-liner call to a copy constructor, you get 'auto' =
for free, and you gain a significant efficiency bump.</div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_787_733241.1377395273666--
.
Author: Jeffrey Yasskin <jyasskin@google.com>
Date: Sat, 24 Aug 2013 19:57:18 -0700
Raw View
On Sat, Aug 24, 2013 at 1:57 PM, Jared Grubb <jared.grubb@gmail.com> wrote:
> I'd like to propose to add a new "weak_from_this" member function to the =
std::enable_shared_from_this class. This has two advantages:
>
> 1. Enables use of 'auto':
> auto weak_handle =3D weak_from_this();
>
> 2. Minor efficiency bump over today's version:
> std::weak_ptr<Foo> weak_handle =3D shared_from_this(); // downgra=
de of a temporary shared_ptr object
>
> I think every implementation of std::enable_shared_from_this is just a wr=
apper of a weak_ptr anyway. Providing a method that copies the internal var=
iable is more efficient than using shared_from_this, which lock()s the inte=
rnal weak_ptr, only to just immediately degrade back to a weak reference.
>
> This is my first attempt at proposing a feature, so forgive my newbness :=
)
Seems like a reasonable idea, although not a slam dunk. I see #2 as
more important for adding to the enable_shared_from_this interface
because it can't be implemented outside of the interface, while
Nicol's "to_weak_ptr" satisfies your #1 and can be implemented outside
of the standard. We'll need a paper in order to actually make the
change, and I think you'd have a higher chance of acceptance if that
paper explores the actual performance impact in 2-3 widely-used
implementations of enable_shared_from_this. You should also try to
find some indication of how often this feature would be used in real
codebases and how performance-critical such uses are. Also see if
anyone (especially standard library maintainers) can think of other
ways to get a similar speedup without adding to the interface.
Re Nicol's "enable_shared_from_this doesn't *have* to wrap weak_ptr",
that's ok, since this feature is implementable even in libraries that
aren't implemented that way; it just might not give as much of a
speedup.
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/ for some
papers you can imitate, and "Paper submissions for the upcoming
mailing" on this mailing list for instructions on submitting a paper.
Jeffrey
--=20
---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.
.