I would like to know how classes A
and B
below can work polymorphically in python when using std::shared_ptr
instead of boost::shared_ptr
?
struct A
{
virtual ~A() {}
};
struct B : A
{
B() {}
virtual ~B() {}
};
void f(const std::shared_ptr<A>& ptr)
{}
BOOST_PYTHON_MODULE(test)
{
class_<A, boost::noncopyable>("A", no_init);
class_<B, std::shared_ptr<B>, bases<A>>("B")
.def(init<>());
def("f", f);
}
I am aware that the the boost::get_pointer
method must be defined for std::shared_ptr
, so I make sure that the following lines exist before a #include <boost/python.hpp>
:
namespace boost {
template<class T> const T* get_pointer(const std::shared_ptr<T>& p)
{
return p.get();
}
template<class T> T* get_pointer(std::shared_ptr<T>& p)
{
return p.get();
}
} // namespace boost
Now, in python I try:
>>> from test import *
>>> b = B()
>>> f(b)
Traceback (most recent call last):
File "<console>", line 1, in <module>
ArgumentError: Python argument types in
test.f(B)
did not match C++ signature:
f(std::shared_ptr<A>)
Note: The code above works fine for boost::shared_ptr
, but I want to stick with the C++11 types instead.
Thanks.