我正在将用 C++ 编写的 Python 模块从 Boost.Python 移动到 Pybind11。我的 C++ 代码依赖于具有不透明指针的 C 库。对于 Boost.Python,我使用此处的文档来处理这些不透明的指针:https ://www.boost.org/doc/libs/1_66_0/libs/python/doc/html/reference/to_from_python_type_conversion/boost_python_opaque_pointer_conv.html
我找不到 Pybind11 的等效代码。作为参考,这是我想使用 Pybind11 向 Python 公开的 C 标头:
typedef struct mytruct* mystruct_t;
void myfunction1(mystruct_t x);
mystruct_t myfunction2();
这可以通过 Boost.Python 公开,如下所示:
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(mystruct)
namespace bpl = boost::python;
BOOST_PYTHON_MODULE(mymodule) {
bpl::opaque<mystruct>();
bpl::def("f1", &myfunction1);
bpl::def("f2", &myfunction2, bpl::return_value_policy<bpl::return_opaque_pointer>());
}