pybind / pybind/pybind11

[BUG]: Cannot wrap class with virtual inheritance and shared_ptr holder?

Open
#3,882 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Required prerequisites
Problem description

The code below throws an error

pybind11.h(1792,1): error C2664: 'std::shared_ptr::shared_ptr(std::nullptr_t) noexcept': cannot convert argument 1 from 'V *' to 'std::nullptr_t

This does not seem expected... This only does it for the bindings of B, not A.

This is different from the other issues I found since in this case the class is actually polymorphic and does inherit from enable_shared_from_this.

Reproducible example code
#include <memory>

#include <pybind11/pybind11.h>

namespace py = pybind11;

class A : public std::enable_shared_from_this<A> {
public:
    virtual ~A() {}
};

class B : public virtual A {
protected:
    ~B() {}
};

PYBIND11_MODULE(module, m) {
    py::class_<A, std::shared_ptr<A>>(m, "A");
    py::class_<B, std::shared_ptr<B>, A>(m, "B");
}

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 reproducible C++ example and inspect pybind11.h around line 1792, where the shared_ptr conversion error is reported. Confirm the failure when binding B with virtual inheritance and its shared_ptr holder; done means the example binds successfully without the reported compiler error.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.