Pybind - 使用派生的共享指针调用函数 Class
Pybind - Invoke Function with Shared-Pointer to Derived Class
我有以下设置(1 个基础 class、1 个派生 class、1 个容器)。容器将 shared_ptr<Base>
作为输入。
#include <pybind11/pybind11.h>
namespace py = pybind11;
struct Base { };
struct Derived : public Base { };
struct Container { void input(const std::shared_ptr<Base>& ptr) { } };
PYBIND11_MODULE(PybindTest, m)
{
py::class_<Base, std::shared_ptr<Base>>(m, "Base").def(py::init<>());
py::class_<Derived, std::shared_ptr<Derived>>(m, "Derived").def(py::init<>());
py::class_<Container, std::shared_ptr<Container>>(m, "Container")
.def(py::init<>())
.def("input", &Container::input);
}
在 C++ 中,我可以将 shared_ptr<Base>
或 shared_ptr<Derived>
传递给 input
函数。但是在 Python 我得到一个错误:
import PybindTest as p
p.Container().input(p.Base()) # All good
p.Container().input(p.Derived()) # Throws Error
# TypeError Traceback (most recent call last)
# <ipython-input-10-70fe5b9f3a41> in <module>
# 1 import PybindTest as p
# 2 p.Container().input(p.Base())
# ----> 3 p.Container().input(p.Derived())
#
# TypeError: input(): incompatible function arguments. The following argument types are supported:
# 1. (self: PybindTest.Container, arg0: PybindTest.Base) -> None
#
# Invoked with: <PybindTest.Container object at 0x0000022378B4FF80>, <PybindTest.Derived object at 0x0000022378B4FCE0>
我尝试过
.def("input", py::overload_cast<const std::shared_ptr<Derived>&> (&Container::input))
.def("input", [](const std::shared_ptr<Derived> & ptr) { this->input(ptr); })
但这两个都不编译。 有什么建议吗?
我将 Windows 10 与 Python 3.6 x64 一起使用,并使用 VS 2019 编译所有内容。
作为@n.m。建议:
py::class_<Derived, std::shared_ptr<Derived>, Base>(m, "Derived")
.def(py::init<>());
来自pybind11 documentation(方法一:模板参数)
我有以下设置(1 个基础 class、1 个派生 class、1 个容器)。容器将 shared_ptr<Base>
作为输入。
#include <pybind11/pybind11.h>
namespace py = pybind11;
struct Base { };
struct Derived : public Base { };
struct Container { void input(const std::shared_ptr<Base>& ptr) { } };
PYBIND11_MODULE(PybindTest, m)
{
py::class_<Base, std::shared_ptr<Base>>(m, "Base").def(py::init<>());
py::class_<Derived, std::shared_ptr<Derived>>(m, "Derived").def(py::init<>());
py::class_<Container, std::shared_ptr<Container>>(m, "Container")
.def(py::init<>())
.def("input", &Container::input);
}
在 C++ 中,我可以将 shared_ptr<Base>
或 shared_ptr<Derived>
传递给 input
函数。但是在 Python 我得到一个错误:
import PybindTest as p
p.Container().input(p.Base()) # All good
p.Container().input(p.Derived()) # Throws Error
# TypeError Traceback (most recent call last)
# <ipython-input-10-70fe5b9f3a41> in <module>
# 1 import PybindTest as p
# 2 p.Container().input(p.Base())
# ----> 3 p.Container().input(p.Derived())
#
# TypeError: input(): incompatible function arguments. The following argument types are supported:
# 1. (self: PybindTest.Container, arg0: PybindTest.Base) -> None
#
# Invoked with: <PybindTest.Container object at 0x0000022378B4FF80>, <PybindTest.Derived object at 0x0000022378B4FCE0>
我尝试过
.def("input", py::overload_cast<const std::shared_ptr<Derived>&> (&Container::input))
.def("input", [](const std::shared_ptr<Derived> & ptr) { this->input(ptr); })
但这两个都不编译。 有什么建议吗?
我将 Windows 10 与 Python 3.6 x64 一起使用,并使用 VS 2019 编译所有内容。
作为@n.m。建议:
py::class_<Derived, std::shared_ptr<Derived>, Base>(m, "Derived")
.def(py::init<>());
来自pybind11 documentation(方法一:模板参数)