如何正确绑定C++ pybind11的克隆模式?

发布于 2025-01-24 18:04:00 字数 1660 浏览 4 评论 0原文

我正在尝试用PyBind11包装C ++类,以实现克隆模式。我一直在关注提供的文档(请参阅 https:///pybind11.readthedocs。 io/en/stable/Advanced/class.html )但是,每当我尝试编译下面的代码时,编译器都会引发错误:

pybind11\include\pybind11\cast.h(1031,12): error C2672: 'cast_op': no matching overloaded function found [...]
pybind11\include\pybind11\cast.h(1105): message : see reference to function template instantiation 'T pybind11::cast<T,0>(const pybind11::handle &)' being compiled [...]
with
[
   T=std::unique_ptr<Base,std::default_delete<Base>>
]
pybind11\include\pybind11\cast.h(1168): message : see reference to function template instantiation 'std::unique_ptr<Base,std::default_delete<Base>> pybind11::cast<T>(pybind11::object &&)' being compiled [...]
with
[
   T=std::unique_ptr<Base,std::default_delete<Base>>
]

代码:

#include <pybind11/pybind11.h>

#include <memory>

class Base
{
public:
    Base()
    {
    }

    virtual std::unique_ptr<Base> Clone() const
    {
        return std::make_unique<Base>(*this);
    }
};

class PyBase : public Base
{
public:
    using Base::Base;

    virtual std::unique_ptr<Base> Clone() const
    {
        PYBIND11_OVERRIDE_NAME(std::unique_ptr<Base>, Base, "clone", Clone);
    }
};

PYBIND11_MODULE(example, module)
{
    pybind11::class_<Base, PyBase>(module, "Base")
        .def(pybind11::init<>())
        .def("clone", &Base::Clone);
}

我如何使用PYBIND11正确地绑定此代码?

I'm attempting to wrap a C++ class implementing the clone pattern with pybind11. I've been following the documentation provided (see https://pybind11.readthedocs.io/en/stable/advanced/classes.html) but whenever I try to compile the code below, the compiler throws an error:

pybind11\include\pybind11\cast.h(1031,12): error C2672: 'cast_op': no matching overloaded function found [...]
pybind11\include\pybind11\cast.h(1105): message : see reference to function template instantiation 'T pybind11::cast<T,0>(const pybind11::handle &)' being compiled [...]
with
[
   T=std::unique_ptr<Base,std::default_delete<Base>>
]
pybind11\include\pybind11\cast.h(1168): message : see reference to function template instantiation 'std::unique_ptr<Base,std::default_delete<Base>> pybind11::cast<T>(pybind11::object &&)' being compiled [...]
with
[
   T=std::unique_ptr<Base,std::default_delete<Base>>
]

Code:

#include <pybind11/pybind11.h>

#include <memory>

class Base
{
public:
    Base()
    {
    }

    virtual std::unique_ptr<Base> Clone() const
    {
        return std::make_unique<Base>(*this);
    }
};

class PyBase : public Base
{
public:
    using Base::Base;

    virtual std::unique_ptr<Base> Clone() const
    {
        PYBIND11_OVERRIDE_NAME(std::unique_ptr<Base>, Base, "clone", Clone);
    }
};

PYBIND11_MODULE(example, module)
{
    pybind11::class_<Base, PyBase>(module, "Base")
        .def(pybind11::init<>())
        .def("clone", &Base::Clone);
}

How do I correctly bind this code with pybind11?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文