[GSoC] __enable_shared_from_this_helper
Tim Shen
timshen@google.com
Fri May 1 06:31:00 GMT 2015
On Thu, Apr 30, 2015 at 10:56 PM, Fan You <youfan.noey@gmail.com> wrote:
> To conclude:
>
> 1. What should I do is to create a new class _Sp_counted_array by merging
> _Sp_counted_ptr and _Sp_counted_deleter.
> So, some design like this?
> template<typename _Ptr, typename _Deleter, typename _Alloc>
> class _Sp_counted_array : public _Sp_counted_base {
> public:
> // implement here
> using _Tp = typename std::remove_pointer<_Ptr>::type;
> private:
> _Tp _array[]; // do init in ctor
What do you mean by this?
> size_t _size;
> _Alloc _alloc;
> _Deleter _del; // may use ebo to reduce size
> };
>
> Then, change __shared_count accordingly, others can remain unchanged.
>
> 2. Usage you mentioned
>>
>> shared_ptr<int[]> p(new int[3]);
>>
>> The shared_count only needs to allocate/deallocate a
>>
>> _Sp_counted_array<int> in both cases, and that has a known size.
>
I don't quite understand this.
> How can _Sp_counted_array<int> get the size when user didn't even pass one?
>
> 3. Actually it reminds me that user can use current std::shared_ptr with
> array as long as the right deleter are provided.
>>
>> #include <memory>
>>
>> int main()
>> {
>> int array[3];
>> std::shared_ptr<int[3]> p(&array, [](void*){});
>> }
>
>
> This will work because the _Tp = int[3] in original shared_ptr
> implementation. But the new implementation, which, _Tp = int; So, I am still
> not sure why size is not needed to be pass to _Sp_counted_array.
>>
>> shared_ptr<int[3]> p(new int[3]);
>
> Didn't specify the size but it do tell you by using int[3] as template
> parameter.
>
I believe there are various ways to implement this. Can you first
briefly describe your overall idea?
Two approaches I came up with, as I describe before:
1) Don't change __shared_ptr and anything in it; purely implement
shared_ptr<int[]> in terms of __shared_ptr<int> with special allocator
and/or deleter. This seems to be the same as your 3.
2) Let __shared_count support array directly, and propagate all
parameters from shared_ptr ctor to __shared_ptr -> __shared_count ->
_Sp_counted_array.
--
Regards,
Tim Shen
More information about the Libstdc++
mailing list