函数回调错误

时间:2019-10-07 18:46:04

标签: pybind11

我在c语言中有一个代码,带有一些如下的函数回调

<hr
    style="width: 100%; color: #d3d3d3; background-color: #d3d3d3; height: 1px;" />
<!-- start items -->
<#list record.item as item>
<table style="margin-top: 10px; width: 100%;">
<#if item.custcol_comments?contains("cream")>
  <#compress>
  <tr>
        <td colspan="12" style="text-align: center;"><span
            style="font-weight: bold; line-height: 10%; color: #333333;">${item.item}</span><br />${item.description}</td>
        <td colspan="4" style="text-align: center;">&nbsp;</td>
        <td colspan="4" style="text-align: center;">${item.amount}</td>
</tr>
 </#compress>
  </#if>
</table>
</#list>
<!-- end items -->
<hr

然后是如下结构

 int (*number) (int i, int j); 

我试图将其绑定如下

  typedef struct _name{
        int sal;
        number details;
  }name;

当我尝试在python中执行以下操作

  py::class_<name>(m,"name")
      .def(py::init<>())
      .def_readwrite("sal",&name::sal)
      .def_property("details",[](name &self){return self.details;},[](name &self,std::function<int(int,int)> func){self.details=*func.target<name>();});

我即将结束隔离故障核心。

1 个答案:

答案 0 :(得分:0)

当您将其传递给C ++时,

pybind11将python函数与临时std::function实例一起包装。

然后std::function::target()返回nullptr,因为存储的可调用对象不是普通的函数指针,请参见https://en.cppreference.com/w/cpp/utility/functional/function/target