This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [GSoC] __enable_shared_from_this_helper
- From: Fan You <youfan dot noey at gmail dot com>
- To: Tim Shen <timshen at google dot com>
- Cc: Jonathan Wakely <jwakely at redhat dot com>, Tim Shen <timshen91 at gmail dot com>, "libstdc++" <libstdc++ at gcc dot gnu dot org>
- Date: Sun, 3 May 2015 19:03:27 -0400
- Subject: Re: [GSoC] __enable_shared_from_this_helper
- Authentication-results: sourceware.org; auth=none
- References: <CALvpekGVZEMtGN5MKVXrArG5cKJhqDQjsfgjdFZYk0=H=7+V_Q at mail dot gmail dot com> <20150428215024 dot GF3618 at redhat dot com> <20150428215349 dot GG3618 at redhat dot com> <CALvpekGhEJfEWdHmtOyco-Ua=sPjk4yhpFFE7Fzffy9cTmnVbA at mail dot gmail dot com> <CAG4ZjN=a9nvQ3CgqRiSze-fbUvh0u69WQQdehS5+mFXsvtSP=A at mail dot gmail dot com> <20150429100344 dot GH3618 at redhat dot com> <CAG4ZjNnFTOtEoSPdzv9Dip8s7O6o4ToL2+N-j_oEuxvrD1spLA at mail dot gmail dot com> <20150430112352 dot GR3618 at redhat dot com> <CAG4ZjNmgtF-7jE17d=e8fK_iMhkDHT783Qv1sYpt6MPKsmLL=A at mail dot gmail dot com> <20150430174827 dot GU3618 at redhat dot com> <CALvpekGSvSvg=WcF4w2r3LQ377azPfsAKfLtA5pFm3OKz6DesA at mail dot gmail dot com> <CAG4ZjNmAeB_CvR_4HXO7y3=FxcsTfAXi95RxeH=c8ngfTic5vA at mail dot gmail dot com> <CALvpekF+Y1C+EgevdyEzgt++OEUz4iyjCdTnA6Ny5Ak=b88HTg at mail dot gmail dot com> <CAG4ZjNktcN84WzhTMx-N7SDCG6DYdcH-P6FwnExd-gjp-RH3RQ at mail dot gmail dot com>
Follow up questions:
> > According to my knowledge, __enable_shared_from_this construct a
> > weak_ptr in order to safely instantiate a new shared_ptr use only this
> > pointer. So, does __enable_shared_from_this_helper has no effect until
> > user trying to do something like this?
> >
> >> class Test : public ___enable_shared_from_this { };
> >>
> >> shared_ptr<Test> (new Test());
>
> Correct.
> It ensures that when an object is owned by a shared_ptr, the weak_ptr
> member shares ownership with that shared_ptr.
However,
ctor of __shared_count only pass the same type of pointer into
> __enable_shared_from_this_helper(_M_refcount, __p, __p);
but __enable_shared_from_this_helper take two different types of ptr.
> (__shared_count&, __enable_shared_from_this* ptr1, _Tp1* ptr2)
Can you give any specific example of using this feature ?
2.
int* a = new int(1);
shared_ptr<int> a1(a);
shared_ptr<int> a2(a);
When user are trying to do some thing like this, should it throw? I've
tested on the original
std::shared_ptr in gcc 4.9.2, it will not throw on type like <int> but
it do throw on array types.
However, my currently implementation will throw "double deletion" at runtime.
<http://stackoverflow.com/questions/10338606/multiple-shared-ptr-storing-same-pointer>
3. Also, we might also need a specialization for
enable_shared_from_this in order to use
shared_from_this() with std::experimental::shared_ptr;
2015-05-01 18:14 GMT-04:00 Tim Shen <timshen@google.com>:
> On Fri, May 1, 2015 at 10:02 AM, Fan You <youfan.noey@gmail.com> wrote:
>> This is can be combined with the first approach right? by merging
>> _Sp_counted_ptr with _Sp_shared_deleter into _Sp_shared_array.
>
> Why it's related to the first approach? First one doesn't even change
> __shared_ptr, if you like;
>
> Second one changes __shared_ptr, __shared_count, and creates a new
> derived class of _Sp_counted_base.
>
>
> --
> Regards,
> Tim Shen