Skip to content

Motivation for "type does not have a non-default holder type while its base does"? #1317

Closed
@BorisSchaeling

Description

@BorisSchaeling

What's the motivation for the if (default_holder != base_info->default_holder) check in add_base()?

I'm asking as I came across this check when I tried to pybind this C++ code:

class Base
{
protected:
  virtual ~Base() = default;
};

class Derived : public Base
{
};

As the Base class has a protected destructor, I used nodelete:

PYBIND11_MODULE(Test, m) {
  class_<Base, std::unique_ptr<Base, nodelete>> base{ m, "Base" };
  class_<Derived, Base> derived{ m, "Derived" };
}

The code compiles but when you try to import the module you get this error message because the if-check fails:

ImportError: generic_type: type "Derived" does not have a non-default holder type while its base "Base" does

Now I could just use for example std::shared_ptr as a holder type for the Derived class to satisfy the condition (as then Derived uses a non-default holder type, too). But I think I better understand what pybind11 is trying to protect me from with this if-check. Or does the if-check make no sense in my use case (as it's actually meant to protect users in another use case)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions