|
Boost : |
Subject: Re: [boost] [interest] rich-typed smart pointers
From: Larry Evans (cppljevans_at_[hidden])
Date: 2013-10-06 08:26:15
On 10/05/13 23:14, Larry Evans wrote:
> On 10/05/13 20:14, Larry Evans wrote:
>> On 10/05/13 16:26, Julian Gonggrijp wrote:
>>> Larry Evans wrote:
>>>
>>>>> https://github.com/jgonggrijp/rich-typed-pointers
>>>> [snip]
>>>> If there were a templated owned_ptr CTOR:
>>>>
>>>> template<typename Derived>
>>>> owner_ptr (owner_ptr<Derived> && source) :
>>>> pointer(source.pointer) {
>>>> source.pointer = nullptr;
>>>> }
>>>>
>>>> wouldn't that eliminate the need for the make_dynamic templated
>>>> function?
>>>>
>>>> Just wondering. I haven't tried it, but I'd guess it would work
>>>> because
>>>> the ownd_ptr<Derived> could be constructed with the:
>>>>
>>>> make<Derived>(t1,t2,...tn)
>>>>
>>>> and then the templated owned_ptr CTOR would then convert that to the
>>>> base class?
>>>
>>> I agree this would be great, but unfortunately it appears it can't be
>>> done.
>>> owner_ptr<Derived> would have to friend-declare owner_ptr<Base>,
>> I don't see why. AFAICT, you'd require *fewer* friend declarations, not
>> more( you wouldn't need the:
>>
>> template <class U1, class U2, class ... Us>
>> friend owner_ptr<U1> make_dynamic (Us&& ...);
>>
>> the only friend needed would the existing:
>>
>> template <class U, class ... Us>
>> friend owner_ptr<U> make (Us&& ...);
>>
>> that's because the proposed templated constructor would have the same
>> access as the existing:
>>
>> owner_ptr (owner_ptr && source);
>>
>> CTOR, i.e. public. IOW, with the proposed templated CTOR, the
>> following example code creating an owner_ptr<Base> should work,
>> AFAICT:
>>
>> owner_ptr<Base>
>> owner_baee
>> ( make<Derived>
>> ( DerivedInit0()
>> , DerivedInit1()
>> .
>> .
>> . DerivedInitN()
>> )
>> );
>>
>>
>> Where DeriveInit0, DerivedInit1, ... DerivedInitN
>> are the types of the args to the CTOR for Derived.
>>
>> Is there some reason why the above example code would
>> *not* compile, given then proposed templated CTOR?
>> [snip]
>>
> Coded it but it failed to compile because
> owner_ptr<shape> could not assign a nullptr to
> owner_ptr<circle>::pointer because (as you mentioned)
> owner_ptr<shape> is not a fried of owner_ptr<circle>.
>
> Sorry for noise.
>
Googling:
c++ templated friend
lead to:
http://stackoverflow.com/questions/3292795/template-friend
which lead to the owner_ptr CTOR code:
template<typename Derived>
owner_ptr (owner_ptr<Derived>&& source) : pointer(source.pointer) {
source.pointer = nullptr;
}
template <class Derived>
friend class owner_ptr;
in rich_typed_ptr.hpp which resulted in successful compile and run
of the shapes.cpp example without using make_dynamic.
HTH.
-regards,
Larry
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk