具有默认参数的包装方法,该方法是指向另一个包装类型的空指针

问题描述

考虑以下C ++代码

struct A { };

struct B {
    void f(A *a = nullptr) const;
};

如何使用boost::python正确包装?特别是认参数。以下内容失败了 1

bpy::class_<B>
    // Explicitly converting the nullptr to A* does not solve this:
    .def("f",&B::f,bpy::arg("a") = nullptr);

1 通过“惨败” ,我的意思是,导入模块时,我只会得到“ SystemError:初始化xxx引发未报告的异常” *。

解决方法

最后,这很简单。只需要将空指针包装在bpy::ptr中:

bpy::class_<B>
    // The cast to A* is mandatory otherwise bpy::ptr cannot deduce the type:
    .def("f",&B::f,bpy::arg("a") = bpy::ptr((A*)nullptr));