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

Sebastian Walter walter at mathematik.hu-berlin.de
Tue Feb 19 11:48:44 CET 2008

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:

/* by_reference.cpp */
#include <boost/python.hpp>

void by_reference(double &x){
	x = 23.;
	using namespace boost::python;
	def("by_reference", by_reference);

/* by_reference.py */
import mymodule

x = 1.
print x
print x

/* output */
>>Traceback (most recent call last):
>>  File "./test.py", line 7, in <module>
>>    mymodule.by_reference(x)
>>Boost.Python.ArgumentError: Python argument types in
>>    mymodule.by_reference(float)
>>did not match C++ signature:
>>    by_reference(double {lvalue})

thanks in advance,
Sebastian Walter

More information about the Cplusplus-sig mailing list