Swig:如何包装double& (双引用)? [英] Swig: How to wrap double& (double passed by reference)?
问题描述
我使用SWIG从Python访问C ++代码。如何优雅地包装一个函数,该函数通过引用传递的变量返回值
I am using SWIG to access C++ code from Python. How do I elegantly wrap a function that returns values in variables passed by reference like
void set(double&a) {
a = 42.;
}
我找不到如何做。在最好的情况下,我可以使用Python中的函数与Python浮动:
I could not find out how to do this. In the best case I'd be able to use the function in Python with Python floats:
>>> b = 2.
>>> set(b)
>>> print b
42.0
现在它给我一个 TypeError :在方法'TestDouble_set'中,类型为'double&'
At the moment it gives me a TypeError: in method 'TestDouble_set', argument 2 of type 'double &'
.
推荐答案
以这种方式:
您的swig界面文件:
Your swig interface file:
%apply double& INOUT { double& a };
void set(double& a);
在python脚本中的用法:
Usage in python script:
a = 0.0
a = set(a)
print a
如果你的函数返回一个东西(而不是一个void),在python中执行下面的操作
If your function returns something (instead of being a void), do the below in python
ret, a = set(a)
查看swig中的类型发布文档。你可以做INPUT,OUTPUT& INOUT用于参数。 HTH
Checkout the documentation for typemaps in swig. You can do INPUT, OUTPUT & INOUT for arguments. HTH
这篇关于Swig:如何包装double& (双引用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!