[C++-sig] how to wrap getter returning shared_ptr<T>

David Abrahams dave at boost-consulting.com
Wed Dec 11 02:42:20 CET 2002


"Langston, Matthew David" <langston at SLAC.Stanford.EDU> writes:

> Hi Dave,
>
> The register_shared_ptr<T>() idea sounds intriguing. If I understood
> your post in message 1456023, then in my "Foo Bar" example I would
> change this:
>
>
> class_< Bar, shared_ptr < Bar > > ( "Bar" ).add_property ("message",
>  &Bar::get_Message );
>
>
> to this
>
>
> class_< Bar > ( "Bar" ).add_property ( "message", &Bar::get_Message );
> register_shared_ptr < Bar > ();
>
>
> Is that right?

Yes.

> I was surprised that Boost.Python originally allowed me to wrap a
> "member function returning shared_ptr" (e.g. my Foo example compiled
> and linked fine), but I received a run-time error message when I tried
> to call that member function from python. 

This is a difference between Boost.Python v1 and Boost.Python v2.  In
v1, there was some compile-time checking of conversion validity
(unfortunately, it relied on non-standard C++).  In v2, all
conversions are only checked at runtime.

> Would forgetting to specify
>
> register_shared_ptr < Bar > ();
>
> have given me a compile or link time error (where forgetting to
> specify the second template argument in "class_< Bar, shared_ptr < Bar
>> >" did not)?

No, and there's pretty much no reasonable way to cause such an error
to occur, AFAICT.

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list