首页 > 解决方案 > 是否可以在 Pybind11 中将 Python 函数作为 C 风格的函数指针传递?

问题描述

阅读 Pybind11 的文档只能清楚地表明 std::function 用于包装 Python 函数并允许 C++ 调用它但是我需要将 Python 函数作为函数指针传递,这可能吗?据我所知,将 std::function 转换为函数指针是不可能的。例如,将 std::function 回调分配给如下所示的函数指针,或者直接通过 pybind11 传递函数指针:

typedef struct
{
 void (*handlerfunc) (int, char const *);
}handler;

在 Pybind11 模块块中,理想情况下我可以这样分配:

 m.def("assignStruct",[](const std::function<void(int number, char const * req)> &f2)
 {

 handler handlerS =
 {
 f2
 }

 });

标签: pythonc++pybind11

解决方案


推荐阅读