0

I have a C++ class which accepts a std::function, and I want to wrap this class for use in python, using cython.

template<typename F>
class A{
    public:
      A(const F& func):_f{boost::make_shared<F>(func)}{}
      boost::shared_ptr<F> _f;
}

typedef A<std::function<double(const double&)>> AInstance;

Now I want to wrap this AInstance function to python usage. The compilation went through, but when I tried to initialize a AInstance object in python using a simple function

def func(a):
    return 1.0

A_instance = AInstance(func)

Error comes out: raise TypeError('GaussHermiteInstance construction failed!')

My question is, what is the correct type in python corresponding to the std::function in C++? How to wrap a std::function in cython from c++ to python? Thanks!

lucasyu
  • 5
  • 2
  • Does [this post](https://stackoverflow.com/questions/39044063/pass-a-closure-from-cython-to-c/39052204#39052204) help you? – DavidW Jul 25 '22 at 12:33
  • Thanks, it helps. I also found a github repo which uses the same idea: https://github.com/hildensia/py_c_py – lucasyu Jul 27 '22 at 15:25

0 Answers0