pybind11基本回调,不兼容的函数签名错误

pybind11 basic callback, incompatible function signature error

我一辈子都无法获得一个非常基本的 Python 回调函数来在使用 pybind11 构建的扩展模块中工作。我正在尝试按照示例 here,但我想我一定是误解了什么。

C++代码如下:

#include <iostream>
#include <functional>
#include <pybind11/pybind11.h>

namespace py = pybind11;

void run_test(const std::function<int(int)>& f)
{
   // Call python function?
   int r = f(5);
   std::cout << "result: " << r << std::endl;
}

PYBIND11_PLUGIN(mymodule)
{
    py::module m("mymodule");
    m.def("run_test", &run_test);
    return m.ptr();
} 

而使用这个模块的Python代码是

import mymodule as mm

# Test function
def test(x):
  return 2*x

mm.run_test(test)

但是我得到这个错误:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    mm.run_test(test)
TypeError: run_test(): incompatible function arguments. The following argument types are supported:
    1. (arg0: std::function<int (int)>) -> None

Invoked with: <function test at 0x2b506b282c80>

为什么它认为函数签名不匹配?我试着匹配这些例子,但我想我一定是误解了什么...

啊好的,我误解了为什么在那个例子中使用 std::function。这是一个更复杂的双回调,其中一个 C++ 函数被传递给 Python,然后返回到 C++,以检查它是否在旅程中幸存下来。对于仅调用 Python 函数,需要使用 py::object 并将结果转换为 C++ 类型(如 here 所述):

void run_test(const py::object& f)
{
   // Call python function?
   py::object pyr = f(5);
   int r = pyr.cast<int>();
   std::cout << "result: " << r << std::endl;
}

我认为您的问题是您忘记了 #include <pybind11/functional.h>