Skip to content

Implicit conversion from Null when inside a vector #1382

@afryt

Description

@afryt

Can't make an implicit conversion from None (py::none or py::object) to my class work.
It is the same issue as #642 but I'd like to provide a non-obscure use case.

In the interface I'm writing I'd like to provide a constructor from a Python list. The elements of the list can be double, string and None (ideally). The constructor being exposed is this one:
A::A(const vector<B>& l)
I expose it using

py::class_<A> m(module, "A");
m.def(py::init<vector<B>>&>())

I'd like to call this using:

A([None, 1.0, "string"])

I registered implicit conversion from double and string to B and it flies, that is I can create object A using A([1.0, 'string']). This works because B has implicit conversions:
B.def(py::init<double>()).def(py::init<std::string>()).def(py::init<>());
and a list of py::implicitly_convertible<double, B>()....

Question: How to I add implicit conversion from Python None to use B() when in a list?

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