This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [GSoC] __enable_shared_from_this_helper


On Sun, May 3, 2015 at 4:47 PM, Tim Shen <timshen@google.com> wrote:
> On Sun, May 3, 2015 at 4:03 PM, Fan You <youfan.noey@gmail.com> wrote:
>> 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 ?
>>
>
> You are looking at the wrong overloading. There're three overloadings declared:
>

Sorry, I take this back :)

For this example:
    struct A : std::enable_shared_from_this<A> { };
    int main() { std::shared_ptr<A> a(new A()); }

`__enable_shared_from_this_helper(_M_refcount, __p, __p)` matches:

>   // Friend of enable_shared_from_this.
>   template<typename _Tp1, typename _Tp2>
>     void
>     __enable_shared_from_this_helper(const __shared_count<>&,
>                      const enable_shared_from_this<_Tp1>*,
>                      const _Tp2*) noexcept;

instead of

>   template<_Lock_policy _Lp>
>     inline void
>     __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
>     { }
>

Notice that the real function body of the matched function is defined
in std::enable_shared_from_this in shared_ptr.h.


-- 
Regards,
Tim Shen


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]