[C++-sig] Re: Return by value/pointer and properties

David Abrahams dave at boost-consulting.com
Fri May 28 20:31:16 CEST 2004

Trevor Blackwell <tlb at anybots.com> writes:

> I wish I understood better how boost.python wraps return values of
> accessors created with def_readwrite or add_property. In neither case is
> there any place to specify a return_value_policy.
> I see empirically that if I define:
> struct bar_t {
>   int buz;
> };
> struct foo_t {
>   bar_t bar;
> };
>     class_<bar_t>("bar_t")
>         .def_readwrite("buz", &bar_t::buz);
>     class_<foo_t>("foo_t")
>         .def_readwrite("bar", &foo_t::bar);
> then calling foo.bar returns by pointer. 

You're not "calling" anything, except indirectly.  What do you
mean "returns by pointer?"  It returns a python int "by value".  It
can't do otherwise.

> (So, for instance, foo.bar.buz=3; print foo.bar.buz; prints 3). But
> is it a custodian_and_ward, or what? 

No, it's a property with a setter and a getter.  Read 
or http://users.rcn.com/python/download/Descriptor.htm

> What if I wanted it to wrap by value? I can do it by using
> add_property and a getter method that returns by value,

That's exactly what you have to do if you want to set the policies
explicitly.  See
http://www.boost.org/libs/python/doc/v2/class.html#class_-spec for the
behavior of def_readwrite and
for the definition of

> but that's kind of cumbersome.

What would be better?

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list