Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-03-21 15:58:06


Boost.Python is not supposed to support the return of non-const references.
Why? Because Python has no way of controlling the lifetime of the referent.
You could easily write code in Python that crashed the program by using an
object returned by reference. A returned const reference can be turned into
a new value under Python's control by copying, and though inefficient,
that's what we do.

I am very surprised that you can get the code to compile at all.

OOPS! Now that I look, I can see that the usual implicit conversion of
non-const to const references is working against us here. All non-const
references are treated as const references, so returned non-const references
also result in the copying of the referent, with the results that you
observed below.

I have been meaning to add a solution to boost.python for dealing with
returned pointers and references in a more explicit way. I guess this just
makes the need more urgent. Thanks for pointing out the problem; I'm sorry
that I don't have an immediate solution. Please hang on a fix.

[In the meantime, there is some outdated documentation at
http://www.boost.org/libs/python/doc/pointers.html under the heading "If you
can't (afford to) copy the referent, or the pointer is non-const" which may
be helpful as a workaround]

-Dave

> -----Original Message-----
> From: charlie_at_[hidden]
> [mailto:charlie_at_[hidden]]
> Sent: Wednesday, March 21, 2001 2:45 PM
> To: boost_at_[hidden]
> Subject: [boost] boost.python simple question
>
>
> If I am exposing these 2 simple classes to Python:
>
> class point
> {
> public:
> float x, y;
> };
>
> class quad
> {
> point p1;
> public:
> void add(const point& p) { p1 = p; }
> point& get() { return p1; }
> };
>
> ... with this code:
>
> python::class_builder<point> point_class
> (this_module, "point");
> point_class.def(python::constructor<>());
> point_class.def_read_write(&point::x, "x");
> point_class.def_read_write(&point::y, "y");
>
> python::class_builder<quad> quad_class(this_module, "quad");
> quad_class.def(python::constructor<>());
> quad_class.def(&quad::add, "add");
> quad_class.def(&quad::get, "get");
>
> ... should there be a simple change that allows this to work:
>
> >>> p = point()
> >>> p.x = 100
> >>> p.y = 125
> >>> q = quad()
> >>> q.add(p)
> >>> q.get().x = 0
> >>> q.get().x
> 0.0
>
> ^ instead it's returning 100.0 (because the get() is returning a copy
> of the point p).
>
> Thanks!
>
> Charlie Barrows
>
>
>
> List-Unsubscribe: <mailto:boost-unsubscribe_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk