Search code examples
c++boost-python

Wrapping method with default argument that is a null pointer to another wrapped type


Consider the following C++ code:

struct A { };

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

How can I wrap this properly using boost::python? In particular the default argument.

The following generates a SystemError (initialization of xxx raised unreported exception) when importing the module:

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

Solution

  • So this was quite... simple, in the end. Just need to wrap the null pointer in a 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));