[GSoC] __enable_shared_from_this_helper
Jonathan Wakely
jwakely@redhat.com
Tue Apr 28 21:50:00 GMT 2015
On 28/04/15 16:48 -0400, Fan You wrote:
>Hi,
>
>1. I feel confused about the function
>__enable_shared_from_this_helper(); in shared_ptr_base.h.
>
>It's defined as (on line 853) and overloaded to 2 extra version which
>accept enable_shared_from_this*:
>
>> template<_Lock_policy _Lp>
>> inline void
>> __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
>> { }
>
>
>Then it get called in every __shared_ptr_base ctors like this,
>
>> __enable_shared_from_this_helper(_M_refcount, __p, __p);
>
>
>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.
>
>2. Also, I am little bit unsure about what _Sp_counted_ptr_inplace does.
>
>It seems to me that when I call allocate_shared<>(); it construct a
>single _Sp_counted_ptr_inplace object.
>Then, _Sp_counted_ptr_inplace is taking charge of allocating the
>needed memory for shared_ptr by using the given allocator.
>
>Does it means, if I want to make make_shared to support array, what I
>need to do is change the constructor of _Sp_counted_ptr_inplace to
>support allocate size?
I haven't thought about it in detail, but I think you'll need a new
type for make_shared<T[]>.
More information about the Libstdc++
mailing list