Passing Python function to Boost C++

前端 未结 1 646
没有蜡笔的小新
没有蜡笔的小新 2021-01-27 03:04

I\'m trying to learn about Boost functions. I want to pass a Python function to a C++ module wrapped using Boost Python. I followed the example given here and modified it to acc

1条回答
  •  遥遥无期
    2021-01-27 04:08

    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(_callable(t));
      }
    
      object _callable;
    };
    
    void setOperator(object obj) {
      op = boost::function( 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.

    0 讨论(0)
提交回复
热议问题