Passing Python function to Boost C++

喜你入骨 提交于 2019-12-02 12:05:19
Aravind

After a lot of searching and some trial & error, I managed to modify my code so that it works. I had to create a wrapper struct for the Boost Python object, which included a operator() method, so that the wrapped object could then be cast as a Boost Function before being assigned to the op variable. Here's the modified code:

struct op_wrapper_t {

  op_wrapper_t( object callable ) : _callable( callable ) {}

  double operator()(double t) {
    return extract<double>(_callable(t));
  }

  object _callable;
};

void setOperator(object obj) {
  op = boost::function<double (double)>( op_wrapper_t(obj) );
}

I followed a similar procedure as the one in this post. However, I still don't understand why no such wrapper/casting is needed for functions that return void.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!