pybind / pybind/pybind11

fail to export class inheriting from std::enable_shared_from_this

Open
#1,771 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

casters
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal Dog example and the reported construction failure at pybind11/pybind11.h:1279. Trace the holder type selected by the class_ declaration and reproduce the error before checking related binding tests; done means the example exports a class inheriting from std::enable_shared_from_this without the reported compilation failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.