[C++-sig] Re: return value policy for returning same python object...

David Abrahams dave at boost-consulting.com
Sun Jun 8 23:16:16 CEST 2003

David Abrahams <dave at boost-consulting.com> writes:

>> Well, you are returning a reference to an internal object of the
>> Python Y object, so return_internal_reference seems entirely appropriate.
> Actually, you're much better off writing:
>     const object
>     do_iadd (back_reference<Y&> self, int other)
>     {
>        self.get() += Y_FromInt(other);
>        return self.source();
>     }
> because it will return the original Python object instead of a new
> one which refers to the same C++ data.  That's what the builtin
> operator support does.

In fact, the other technique will work sometimes but will fail
miserably whenever the Python Y object does not wrap a C++ Y that was
constructed from Python (e.g. when it is just pointing to an actual
C++ Y returned, e.g. via return_internal_reference).  In that case,
the Y object can't be converted into a Y_Wrapper to use for the this
argument... because, of course, it isn't one.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list