[C++-sig] passing by non-const reference?

Jim Bosch talljimbo at gmail.com
Thu Feb 19 14:25:30 CET 2015


On Wed, Feb 18, 2015 at 5:13 PM, Christopher O'Grady <cpo at slac.stanford.edu>
wrote:

>
> Hi,
>
> Based on websearches we have done, we believe that boost-python does
> not allow passing arguments by non-const-reference (at least, not
> easily).  Can someone confirm this is still the case, or point me to
> some documentation that discusses it?  We think this prevents us from
> return multiple arguments from a method call.
>
> Looking through the mailing list archives, we believe this is
> impossible.  If someone knows differently can you let us know?
>
>
Passing Boost.Python-wrapped C++ classes wrapped by non-const-reference
works just fine.  What doesn't work is passing immutable Python primitive
types by non-const-reference (e.g. numeric types, str).  The reason is that
Python itself doesn't support output arguments, so it's actually impossible
to change the value of, say, an integer, when it's passed to a wrapped
function, and the assumption is that any C++ function that takes a
non-const-reference does indeed intend to modify that argument, and hence
that shouldn't be allowed.

Note that this is a Python limitation, not a Boost.Python one - any wrapper
generator library will have a problem with this case, though it may not be
the same problem.  While Boost.Python refuses to wrap such code, others may
allow it but generate incorrect wrappers that don't modify the output
argument in Python.

The solution is to modify these interfaces to return output arguments in a
tuple, as in the example Stefan just provided.

Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20150219/bd0ef8d0/attachment.html>


More information about the Cplusplus-sig mailing list