Skip to content

Bindings for copy constructors for classes with Python call-back structure #202

@lyskov

Description

@lyskov

Hi Wenzel, I noticed that attempt to bind copy constructors for classes that binded though Python call-back structure (like example12) gives a compilation error. However changing constructor argument type from original class to type-of-call-back structure fixes the error. Is this expect behavior? If so, then perhaps it is a good idea to mention this in docs?

Here is code for modified init_ex12 from PyBind11 example12 to demonstrate this issue. Uncommenting the line .def(py::init<Example12 const &>()) (and commenting out .def(py::init<PyExample12 const &>())) will lead to a compilation error. (rest of example12 code is unchanged)

void init_ex12(py::module &m) {
    /* Important: use the wrapper type as a template
       argument to class_<>, but use the original name
       to denote the type */
    py::class_<PyExample12>(m, "Example12")
        /* Declare that 'PyExample12' is really an alias for the original type 'Example12' */
        .alias<Example12>()
        .def(py::init<int>())

        //.def(py::init<Example12 const &>()) // give compilation error
        .def(py::init<PyExample12 const &>()) // works fine


        /* Reference original class in function definitions */
        .def("run", &Example12::run)
        .def("run_bool", &Example12::run_bool)
        .def("pure_virtual", &Example12::pure_virtual);

    m.def("runExample12", &runExample12);
    m.def("runExample12Bool", &runExample12Bool);
    m.def("runExample12Virtual", &runExample12Virtual);
}

Thanks,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions