pybind / pybind/pybind11

Bug with inheritance, shared_ptr holder and enable_shared_from_this

Open
#1,790 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Issue description

error C2683: 'dynamic_cast': 'base' is not a polymorphic type

If base derives from std::enable_shared_from_this, pybind11 uses dynamic_cast, even if type is not polymorphic.

Reproducible example code

#include <memory>
#include <pybind11/pybind11.h>

struct base : public std::enable_shared_from_this<base>
{
};

struct derived : base
{
};

void python_export(pybind11::module& m)
{
	pybind11::class_<base, std::shared_ptr<base>>(m, "Base")
		.def(pybind11::init<>())
	;

	pybind11::class_<derived, base, std::shared_ptr<derived>>(m, "Derived")
		.def(pybind11::init<>())
	;
}

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 by compiling and running the reproducible C++ example with pybind11, focusing on the shared_ptr holder and inheritance path for base and derived. Trace where enable_shared_from_this causes dynamic_cast to be used and verify the behavior for a non-polymorphic base. Done means the example compiles without C2683 while preserving the shared_ptr inheritance behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.