pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

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

Open Holt59 opened this issue 3 years ago • 1 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [x] Consider asking first in the Gitter chat room or in a Discussion.

Problem description

The code below throws an error

pybind11.h(1792,1): error C2664: 'std::shared_ptr<B>::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");
}

Holt59 avatar Apr 18 '22 15:04 Holt59

So I misunderstood the issue and the problem is constructing a std::shared_ptr<B> from a B* because the destructor of B is private.

I don't know why this is necessary in pybind11 - I only want to expose the interface to Python, all construction will be done on the C++ side, so there is never a need to construct a shared_ptr<B> from something else than a shared_ptr<B>. Is there a way to tell pybind11 that?

Holt59 avatar Apr 18 '22 16:04 Holt59