Skip to content

fail to export class inheriting from std::enable_shared_from_this #1771

Open
@vryy

Description

@vryy

Hello

I got an error when exposing a class inheriting from std::enable_shared_from_this. The code is below

struct Dog : public std::enable_shared_from_this<Dog> {
    Dog(const std::string &name) : name(name) { }
    void setName(const std::string &name_) { name = name_; }
    const std::string &getName() const { return name; }
    std::string name;
};

PYBIND11_MODULE(example, m) {
    pybind11::class_<Dog>(m, "Dog")
        .def(pybind11::init<const std::string &>())
        .def("setName", &Pet::setName)
        .def("getName", &Pet::getName);
}

The error I have is:

/home/hbui/sw/pybind11/include/pybind11/pybind11.h:1279:17: error: no matching function for call to ‘std::unique_ptr<Dog, std::default_delete<Dog> >::unique_ptr(std::remove_reference<std::shared_ptr<Dog>&>::type)’
             new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(sh));
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Can anyone suggest a solution for it? Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    castersRelated to casters, and to be taken into account when analyzing/reworking casters

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions