如何正确绑定C++ pybind11的克隆模式?
我正在尝试用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论