Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-02-05 18:50:51

As Alex Martelli recently discovered, BPL will convert const reference
return types to_python by /copying/ the referent into an object managed by a
python reference count.

BPL refuses to automatically convert non-const reference return types
to_python because there is no way to give Python control over the lifetime
of the referent and still maintain the property that the original object is
modified through mutating operations.

It is similar to the problem described in for non-const pointer return
types. You can employ the same (dangerous) workaround described under "If
you can't (afford to) copy the referent, or the pointer is non-const" if

  PyObject* to_python(Foo& p)
      return boost::python::PyExtensionClassConverters::ptr_to_python(&p);

To what is the reference referring? If it refers to an object owned by one
of the arguments to the function you're wrapping (including "this", if it's
a member function) I could give you a way to return the non-const reference
safely; it would work by managing a reference count on the owning object. I
have offered this to several people in the past, but demand has not been
great so far.


----- Original Message -----
From: <l.dunn_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Monday, February 05, 2001 6:11 PM
Subject: [boost] BPL: How do I return a non-const reference?

> My code has about 20 functions that return non-const references to
> wrapped classes. I'm getting lots of "from_python" errors.
> Apologies if this is a stupid question- I've been coding with boost
> for about 3 weeks.
> Thanks in advance!

Boost list run by bdawes at, gregod at, cpdaniel at, john at