Topic: Notification function for shared_from_this for


Author: "'Johannes Schaub' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Mon, 16 May 2016 17:56:19 +0200
Raw View
The need of "virtual" can be mitigated by using the fact that
enable_shared_from_this is actually a CRTP base. So it suffices if it
called static_cast<T*>(ptr)->attached(), and T may decide on its own
whether it wants it virtual or not.

2016-05-16 17:51 GMT+02:00 Johannes Schaub <schaub.johannes@googlemail.com>:
> I was thinking about constructors of classes that derive from
> enable_shared_from_this. A problem with these classes is that the
> shared_from_this() function is not available from the constructor,
> because only afterwards, the object will be owned by a shared_ptr
>
>     class F : public enable_shared_from_this {
>     public:
>         F() {
>            doSomething(shared_from_this());
>         }
>     }
>
>     int main() { std::make_shared<F>(); }
>
> The code is broken. However, if shared_ptr called a function after it
> attached to F, this can be written
>
>     class F : public enable_shared_from_this {
>     private:
>         virtual void attached() {
>            doSomething(shared_from_this());
>         }
>     }
>
>     int main() { std::make_shared<F>(); }
>
> What are your thoughts on this?

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANu6V4WjPSTTtOSj%3D0ML%3DNpaU9dWEpLT2SoY%2BOSq5qOH7MA2BA%40mail.gmail.com.

.


Author: Bjorn Reese <breese@mail1.stofanet.dk>
Date: Mon, 16 May 2016 19:07:48 +0200
Raw View
On 05/16/2016 05:51 PM, 'Johannes Schaub' via ISO C++ Standard - Future
Proposals wrote:

> What are your thoughts on this?

You can do something similar today. Make your constructors private and
instantiate objects via a factory method:

     class F : public enable_shared_from_this<F> {
     public:
         template <typename... Types>
         static std::shared_ptr<F> create(Types&&... args) {
             // std::make_shared does not like private constructors
             auto result = std::shared_ptr<F>(new
F{std::forward<Types>(args)...});
             doSomething(result);
             return result;
         };
     private:
         F() = default;
     };

     int main() { F::create(); }

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5739FE64.1070504%40mail1.stofanet.dk.

.


Author: "'Johannes Schaub' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Mon, 16 May 2016 19:10:20 +0200
Raw View
Thanks, this is a valid workaround which may not be obvious.

However, it requires boiler-plate code and requires making the
constructor private. Inheriting from "F" now makes the derived class
need a factory function aswell with the same boilerplate code. The
very reason for me to use enable_shared_from_this is to avoid such
boilerplate.


2016-05-16 19:07 GMT+02:00 Bjorn Reese <breese@mail1.stofanet.dk>:
> On 05/16/2016 05:51 PM, 'Johannes Schaub' via ISO C++ Standard - Future
> Proposals wrote:
>
>> What are your thoughts on this?
>
>
> You can do something similar today. Make your constructors private and
> instantiate objects via a factory method:
>
>     class F : public enable_shared_from_this<F> {
>     public:
>         template <typename... Types>
>         static std::shared_ptr<F> create(Types&&... args) {
>             // std::make_shared does not like private constructors
>             auto result = std::shared_ptr<F>(new
> F{std::forward<Types>(args)...});
>             doSomething(result);
>             return result;
>         };
>     private:
>         F() = default;
>     };
>
>     int main() { F::create(); }

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANu6V4UtvozOfumbm8prCVOLm8Wjkxxt13xaOVFd_ts7Ow-GGA%40mail.gmail.com.

.


Author: Jakob Riedle <jakob.riedle@gmail.com>
Date: Sat, 21 May 2016 09:34:15 -0700 (PDT)
Raw View
------=_Part_349_980217733.1463848455285
Content-Type: multipart/alternative;
 boundary="----=_Part_350_1257815123.1463848455292"

------=_Part_350_1257815123.1463848455292
Content-Type: text/plain; charset=UTF-8

Since not being able to construct a shared_ptr pointing to *this in a
constructor is a major downside in my opinion, I've written a *drop-in
replacement* for shared_ptr and enable_shared_from_this:

https://sourceforge.net/projects/shared-from-this-ctor/

The only drawback is that it's not thread safe. For all single threaded
purposes, it works really well. I would really like to see this
functionality in some future version of C++, since this proof of concept
does not require any more memory, nor is it significantly slower. By the
way: The only reason for it not being thread-safe is, that I don't want to
add any asm crack-pottery to my code.

Hope, I could help!

Cheers, Jakob

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3687646a-55a2-46b3-a056-d2cdcb65c797%40isocpp.org.

------=_Part_350_1257815123.1463848455292
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Since not being able to construct a shared_ptr pointi=
ng to *this in a constructor=C2=A0is a major downside in my opinion, I&#39;=
ve written a <b>drop-in replacement</b> for shared_ptr and enable_shared_fr=
om_this:</div><div><br></div><div>https://sourceforge.net/projects/shared-f=
rom-this-ctor/</div><div><br></div><div>The only drawback is that it&#39;s =
not thread safe. For all single threaded purposes, it works really well. I =
would really like to see this functionality in some future version of C++, =
since this proof of concept does not require any more memory, nor is it sig=
nificantly slower. By the way: The only reason for it not being thread-safe=
 is, that I don&#39;t want to add any asm crack-pottery to my code.</div><d=
iv><br></div><div>Hope, I could help!</div><div><br></div><div>Cheers, Jako=
b</div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/3687646a-55a2-46b3-a056-d2cdcb65c797%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/3687646a-55a2-46b3-a056-d2cdcb65c797=
%40isocpp.org</a>.<br />

------=_Part_350_1257815123.1463848455292--

------=_Part_349_980217733.1463848455285--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Sat, 21 May 2016 15:07:43 -0700 (PDT)
Raw View
------=_Part_407_837713082.1463868463268
Content-Type: multipart/alternative;
 boundary="----=_Part_408_1827179132.1463868463268"

------=_Part_408_1827179132.1463868463268
Content-Type: text/plain; charset=UTF-8



On Saturday, May 21, 2016 at 12:34:15 PM UTC-4, Jakob Riedle wrote:
>
> Since not being able to construct a shared_ptr pointing to *this in a
> constructor is a major downside in my opinion, I've written a *drop-in
> replacement* for shared_ptr and enable_shared_from_this:
>
> https://sourceforge.net/projects/shared-from-this-ctor/
>
> The only drawback is that it's not thread safe. For all single threaded
> purposes, it works really well. I would really like to see this
> functionality in some future version of C++, since this proof of concept
> does not require any more memory, nor is it significantly slower. By the
> way: The only reason for it not being thread-safe is, that I don't want to
> add any asm crack-pottery to my code.
>

You don't have to add assembly to make it thread-safe (or rather, as
thread-safe as `shared_ptr`). As far as I understand, that can be done via
atomic operations on its counters.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/248e26c4-c760-439a-b380-1f1e764be113%40isocpp.org.

------=_Part_408_1827179132.1463868463268
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Saturday, May 21, 2016 at 12:34:15 PM UTC-4, Ja=
kob Riedle wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margi=
n-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"l=
tr"><div>Since not being able to construct a shared_ptr pointing to *this i=
n a constructor=C2=A0is a major downside in my opinion, I&#39;ve written a =
<b>drop-in replacement</b> for shared_ptr and enable_shared_from_this:</div=
><div><br></div><div><a href=3D"https://sourceforge.net/projects/shared-fro=
m-this-ctor/" target=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=
=3D&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fsourceforge.net%2Fpro=
jects%2Fshared-from-this-ctor%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFJ=
itC74Zu718MNFeTmdAD-L5klmQ&#39;;return true;" onclick=3D"this.href=3D&#39;h=
ttps://www.google.com/url?q\x3dhttps%3A%2F%2Fsourceforge.net%2Fprojects%2Fs=
hared-from-this-ctor%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFJitC74Zu71=
8MNFeTmdAD-L5klmQ&#39;;return true;">https://sourceforge.net/<wbr>projects/=
shared-from-this-<wbr>ctor/</a></div><div><br></div><div>The only drawback =
is that it&#39;s not thread safe. For all single threaded purposes, it work=
s really well. I would really like to see this functionality in some future=
 version of C++, since this proof of concept does not require any more memo=
ry, nor is it significantly slower. By the way: The only reason for it not =
being thread-safe is, that I don&#39;t want to add any asm crack-pottery to=
 my code.</div></div></blockquote><div><br>You don&#39;t have to add assemb=
ly to make it thread-safe (or rather, as thread-safe as `shared_ptr`). As f=
ar as I understand, that can be done via atomic operations on its counters.=
</div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/248e26c4-c760-439a-b380-1f1e764be113%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/248e26c4-c760-439a-b380-1f1e764be113=
%40isocpp.org</a>.<br />

------=_Part_408_1827179132.1463868463268--

------=_Part_407_837713082.1463868463268--

.


Author: Jakob Riedle <jakob.riedle@gmail.com>
Date: Mon, 23 May 2016 08:22:38 -0700 (PDT)
Raw View
------=_Part_734_936484241.1464016958877
Content-Type: text/plain; charset=UTF-8

Hey Nicol,

How do you do atomic instructions? It would be very nice to implement that. Could you possibly make an example? Of course, if you'd like to do it yourself, I'd appreciate it, but since I've no clue on your time plan, an example would already help a lot.

Jakob

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/6cb9cee9-6342-4c46-869f-cfe9067bb219%40isocpp.org.

------=_Part_734_936484241.1464016958877--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Mon, 23 May 2016 12:04:10 -0700 (PDT)
Raw View
------=_Part_533_398207456.1464030250601
Content-Type: multipart/alternative;
 boundary="----=_Part_534_1005103062.1464030250602"

------=_Part_534_1005103062.1464030250602
Content-Type: text/plain; charset=UTF-8

On Monday, May 23, 2016 at 11:22:39 AM UTC-4, Jakob Riedle wrote:
>
> Hey Nicol,
>
> How do you do atomic instructions?
>

You use the various tools in the atomic header
<http://en.cppreference.com/w/cpp/header/atomic>, introduced in C++11.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/b5230dce-ad62-4b3c-b808-cb54aa90edaf%40isocpp.org.

------=_Part_534_1005103062.1464030250602
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Monday, May 23, 2016 at 11:22:39 AM UTC-4, Jakob Riedle=
 wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.=
8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Hey Nicol,<p>How do you=
 do atomic instructions?</p></blockquote><div><br>You use the <a href=3D"ht=
tp://en.cppreference.com/w/cpp/header/atomic">various tools in the atomic h=
eader</a>, introduced in C++11.</div><br></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/b5230dce-ad62-4b3c-b808-cb54aa90edaf%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/b5230dce-ad62-4b3c-b808-cb54aa90edaf=
%40isocpp.org</a>.<br />

------=_Part_534_1005103062.1464030250602--

------=_Part_533_398207456.1464030250601--

.


Author: Jakob Riedle <jakob.riedle@gmail.com>
Date: Tue, 24 May 2016 10:17:33 -0700 (PDT)
Raw View
------=_Part_2728_163734285.1464110253597
Content-Type: multipart/alternative;
 boundary="----=_Part_2729_1151665287.1464110253598"

------=_Part_2729_1151665287.1464110253598
Content-Type: text/plain; charset=UTF-8



> You use the various tools in the atomic header
> <http://en.cppreference.com/w/cpp/header/atomic>, introduced in C++11.


Thanks! I will add them ASAP.

Jakob

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/df5945e8-5b9d-4656-8e69-255980fc2edc%40isocpp.org.

------=_Part_2729_1151665287.1464110253598
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div><br><blockquote class=3D"gmail_quote" style=3D"m=
argin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left-color: rgb(204, 20=
4, 204); border-left-width: 1px; border-left-style: solid;">You use the <a =
onmousedown=3D"this.href=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2=
Fen.cppreference.com%2Fw%2Fcpp%2Fheader%2Fatomic\x26sa\x3dD\x26sntz\x3d1\x2=
6usg\x3dAFQjCNGYVWuO6rP0Z-n3l9TPhMuXhY8c1A&#39;;return true;" onclick=3D"th=
is.href=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fen.cppreference.=
com%2Fw%2Fcpp%2Fheader%2Fatomic\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGYV=
WuO6rP0Z-n3l9TPhMuXhY8c1A&#39;;return true;" href=3D"http://en.cppreference=
..com/w/cpp/header/atomic" target=3D"_blank" rel=3D"nofollow">various tools =
in the atomic header</a>, introduced in C++11.</blockquote></div></div><div=
><br></div><div>Thanks! I will add them ASAP.</div><div><br></div><div>Jako=
b<br></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/df5945e8-5b9d-4656-8e69-255980fc2edc%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/df5945e8-5b9d-4656-8e69-255980fc2edc=
%40isocpp.org</a>.<br />

------=_Part_2729_1151665287.1464110253598--

------=_Part_2728_163734285.1464110253597--

.