[C++-sig] shared_ptr converters

David Abrahams dave at boost-consulting.com
Mon Apr 21 21:18:04 CEST 2003

Nicodemus <nicodemus at globalite.com.br> writes:

> David Abrahams wrote:
>>Nicodemus <nicodemus at globalite.com.br> writes:
>>>David Abrahams wrote:
>>>>Nicodemus <nicodemus at globalite.com.br> writes:
>>>> However, there is one significant difference: if you don't specify
>>>>shared_ptr<Y> then you will not be able to pass a Python X object as a
>>>>shared_ptr<Y>& argument.  Among other things that means you will not
>>>>be able to expose member functions of the shared_ptr as methods of the
>>>>wrapped X (see Raoul Gough's recent postings about proxies).  You will
>>>>still be able to pass a Python X object as shared_ptr<X>,
>>>>shared_ptr<Y>, shared_ptr<X> const&, or shared_ptr<Y> const&.
>>>You mean that a function that receives a shared_ptr<X>& wouldn't work?
>>>Because the following does:
>>>int Test(boost::shared_ptr<A>& a)
>>>    return a->f();
>>>}          >>> from test import *
>>> >>> a = New()
>>> >>> Test(a)
>>> Or am I misunderstood your response?
>>Yes, my response was badly-phrased.  I should've said: if you don't
>>specify shared_ptr<X>, you won't be able to pass an X object
>>_constructed from Python_ to a function expecting shared_ptr<X>&:
>>             >>> a = A()
>>             >>> Test(a)
>>             Traceback...
> I see... then that solution doesn't solve this more common case (at
> least more common than trying to use shared_ptr's with virtual wrappers,
> as in my original post). 

Do you really think it's so common?

    int Test1(boost::shared_ptr<A> a)
        return a->f();

    int Test2(boost::shared_ptr<A> const& a)
        return a->f();

    >>> from test import *
    >>> Test1(A()) # OK
    >>> Test2(A()) # OK
    >>> Test(A())  # Oops
    Traceback ...

It's pretty unusal, IMO, to pass shared_ptr by *non-const* reference.

> I believe that reverting Pyste back to specify the shared_ptr as
> holder for class_ is the better thing to do for now.  What do you
> think Dave? Thanks for helping!

I'm not convinced.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list