[C++-sig] pass float from python to c++ as reference

Gustavo Carneiro gjcarneiro at gmail.com
Tue Feb 19 12:58:49 CET 2008


On 19/02/2008, Sebastian Walter <walter at mathematik.hu-berlin.de> wrote:
>
> Hello,
> I need to pass a float from Python to C++ as a reference. What is the best
> way to do that? I can't find it in the unit tests:
> boost_1_34_1/libs/python/test/test_builtin_converters.cpp
>
>
> /* by_reference.cpp */
> #include <boost/python.hpp>
>
> void by_reference(double &x){
>         x = 23.;
> }


I do not know anything boost::python specific, but generally the way to
handle references in language bindings is:
   1- You tell the language bindings the "direction" of the parameter in
question.  Direction can be in/out/inout.
   2- The wrapper needs convert an input float parameter, before calling the
C++ function, if direction is in or inout;
   3- The wrapper needs to return the float parameter to the caller (either
as a single value or in a tuple), when returning, if direction is inout or
out.

So it may be true that BP does not support this (I saw Stefan's email), but
it is not true that it cannot be supported (pybindgen does it).  In your
example:

void by_reference(double &x){
        x = 23.;
}

Could be mapped into python as:

x = by_reference ();

-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080219/b6a1ae94/attachment.htm>


More information about the Cplusplus-sig mailing list