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

Posted

技术标签:

【中文标题】带有默认参数的包装方法,该参数是指向另一个包装类型的空指针【英文标题】:Wrapping method with default argument that is a null pointer to another wrapped type 【发布时间】:2020-10-25 17:32:08 【问题描述】:

考虑以下 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 的初始化引发未报告的异常”*。

【问题讨论】:

【参考方案1】:

所以这很...简单,最后。只需要将空指针包装在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));

【讨论】:

以上是关于带有默认参数的包装方法,该参数是指向另一个包装类型的空指针的主要内容,如果未能解决你的问题,请参考以下文章