[C++-sig] pass function pointer to c extension function in python
Qinfeng(Javen) Shi
shiqinfeng at gmail.com
Wed Oct 4 11:39:53 CEST 2006
Dear All,
I used a global function pointer op, so that when op = add, it operates as
add, when op = minus, it operates as minus. I used a function choose_op() to
set op = add or minus. It works if I call choose_op() inside other function
exposed in python. It reported a error when I used bjam to compile it if I
exposed it into python.
Any suggestion will be appreciated. Thanks in advance.
---------------------------c extension---------------------
float add(float a, float b){
return a+b;
}
float minus(float a, float b){
return a-b;
}
float (*op)(float a, float b) = add;
void choose_op(float (* method)(float a, float b)){
op = method;
}
int funcPtrTest()
{
float a,b,c;
a = 1;
b = 2;
c = -10;
choose_op(minus);
c = op(a,b);
printf("%f\n",c);
return 0;
}
BOOST_PYTHON_MODULE(hello){
def("funcPtrTest", &funcPtrTest);
def("choose_op",&choose_op);// this line makes bjam report an error. Without
it is ok. But without it also means we can't call it directly in python.
}
------------------------result-------------
(1)if without def("choose_op",&choose_op),
>>> import hello
>>> c = hello.funcPtrTest()
-1.000000
so it works.
(2)if with def("choose_op",&choose_op),
It can't be compiled successfully by bjam.
/boost_1_33_1/boost/type_traits/add_cv.hpp:34: error: `const
volatile' qualifiers cannot be applied to `float ()(float, float)'
--
Qinfeng(Javen) Shi
Research School of Information Sciences and Engineering
Australian National University
Locked Bag 8001
Canberra ACT 2601
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20061004/f56ad8e7/attachment.htm>
More information about the Cplusplus-sig
mailing list