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

David Abrahams dave at boost-consulting.com
Wed Jun 11 02:05:54 CEST 2003

"Milind Patil" <milind_patil at hotmail.com> writes:

> "David Abrahams" <dave at boost-consulting.com> wrote in message
> news:uwufud67g.fsf at boost-consulting.com...
>> > .def("__iadd__", (object (*)(back_reference<Y&>, int) )&do_iadd)
>>                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> Why this dangerous cast?  It's completely unneccessary, and I
>> cautioned you against it.
> What if this function is overloaded eg. also have
> do_iadd (back_reference<Y&> self, char* other)
> {
>         self.get() += Y_FromString(other);
>        return self.source();
> }
> Having a single
> .def("__iadd__", &do_iadd)
> invokes compilation error...
> :: no matching function for call to `boost::python::class_<Y, <unnamed>::Y_Wrapper,
> boost::python::detail::not_specified, boost::python::detail::not_specified>::def(const char[9],
> <unknown type>)'
> So, is this the recommended thing: explicit cast if overloaded, else implicit?

No, see the tutorial
http://www.boost.org/libs/python/doc/tutorial/doc/overloading.html, or
you can use the (undocumented) boost::implicit_cast<...> form which is
also safe.

> Thanks,
> Milind
> PS: Pyste does explicit casts for overloaded methods.

Pyste probably should use implicit_cast instead, because some people
will use its output as a way of getting started with hand-written
wrappers, but it's not really a big problem since Pyste has the
advantage of a C++ compiler behind it: it always gets the cast right.
Humans are much more fallible.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list