pybind / pybind/pybind11

Issue with method returning std::unique_ptr combined with trampoline class

Open
#1,962 1 comment 1 reaction 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

Hello,
I encountered the problem with method returning std::unique_ptr. When exposed without trampoline class, everything works, however when trampoline class is involved, the code does not compile and compiler complains for missing type_caster. The behavior is illustrated on attached minimal example.

#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
namespace py = pybind11;

class Status {
public:
  Status() {}
};

class Factory {
public:
  Factory() {}
  virtual std::unique_ptr<Status> GetStatus() const {
    return std::unique_ptr<Status> (new Status);
  }
};

template <class FactoryBase = Factory> class PyFactory: public FactoryBase {
public:
  using FactoryBase::FactoryBase;
  std::unique_ptr<Status> GetStatus() const override {
    PYBIND11_OVERLOAD(std::unique_ptr<Status> , FactoryBase, GetStatus, );
  }
};


PYBIND11_MODULE(demo2, m) {

  py::class_<Status>(m, "Status")
    .def(py::init<>())
    ;

  py::class_<Factory, PyFactory<>>(m, "Factory")
    .def(py::init<>())
    .def("GetStatus", &Factory::GetStatus)
    ;
}

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 the attached minimal example and compare the binding with and without the trampoline class. Investigate the PYBIND11_OVERLOAD path for the std::unique_ptr return and the reported missing type_caster; done means the trampoline version compiles and preserves the demonstrated return behavior.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.