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

Langston, Matthew David langston at SLAC.Stanford.EDU
Wed Dec 11 00:09:32 CET 2002


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?

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. 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)?

-Matt

-----Original Message-----
From: David Abrahams [mailto:dave at boost-consulting.com] 
Sent: Monday, December 09, 2002 7:53 PM
To: c++-sig at python.org
Subject: Re: [C++-sig] how to wrap getter returning shared_ptr<T>

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

> I answered my own question through trial-and-error experimentation. If
> I want to have Boost.Python return a "shared_ptr < Bar >", then I must
> export Bar to python using the second template argument to class_ as
> follows:
>
> class_< Bar, shared_ptr < Bar > > ( "Bar" ).add_property ("message",
> &Bar::get_Message);
>
> Matt

<snip>

> I naively expected Boost.Python to just "do the right thing" w.r.t. a
> member function returning a boost::shared_ptr. What am I doing wrong?
>
> Warmest regards, Matt

Matt, 

Please see http://aspn.activestate.com/ASPN/Mail/Message/1456023
(especially the non-quoted section at the bottom).

I've been waiting for more feedback about the issues discussed
there. So far I'm leaning fairly strongly away from the "just do the
right thing" approach and toward the register_shared_ptr<T>()
approach on the "don't pay for what you don't use" theory.

-Dave

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


_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig




More information about the Cplusplus-sig mailing list