On 19/02/2008, <b class="gmail_sendername">Sebastian Walter</b> <<a href="mailto:walter@mathematik.hu-berlin.de">walter@mathematik.hu-berlin.de</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello,<br>I need to pass a float from Python to C++ as a reference. What is the best<br>way to do that? I can't find it in the unit tests:<br>boost_1_34_1/libs/python/test/test_builtin_converters.cpp<br><br><br>/* by_reference.cpp */<br>
#include <boost/python.hpp><br><br>void by_reference(double &x){<br> x = 23.;<br>}</blockquote><div><br>I do not know anything boost::python specific, but generally the way to handle references in language bindings is:<br>
1- You tell the language bindings the "direction" of the parameter in question. Direction can be in/out/inout.<br> 2- The wrapper needs convert an input float parameter, before calling the C++ function, if direction is in or inout;<br>
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.<br><br>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:<br>
</div></div><br>void by_reference(double &x){<br> x = 23.;<br>}<br>
<br>Could be mapped into python as:<br><br>x = by_reference ();<br><br>-- <br>Gustavo J. A. M. Carneiro<br>INESC Porto, Telecommunications and Multimedia Unit<br>"The universe is always one step beyond logic." -- Frank Herbert