[C++-sig] Calling func that takes intrusive_ptr<Base> with intrusive_ptr<Derived>...

Mark Williams mark at image-engine.com
Fri Oct 20 02:18:38 CEST 2006


I found the  query below in the December 2005 archive which would appear to be describing the exact same problem as my own. 

A follow-up by the original author suggested they'd made their own version of converter/shared_ptr_from_python.hpp in order
to support their own SmartPtr type, but they were questioning whether this was indeed the correct path to take.

I've not been able to find replies to either of these original queries so would be grateful for any pertinent information regarding
intrusive_ptr usage. Does Boost.Python support intrusive_ptr to the same level as shared_ptr?  The documentation for
register_ptr_to_python doesn't seen to suggest any particular dependency on any one shared pointer type.

Thanks in advance,

Mark

>
> Hmm.. I was expecting this to work.  Any ideas as to why it doesn't?
>
>Thanks,
>
>Alex
>
>
>#include <boost/python.hpp>
>#include <boost/smart_ptr.hpp>
>#include <string>
>
>using namespace boost::python;
>using boost::intrusive_ptr;
>using std::string;
>
>class Base { public: virtual ~Base() {} };
>class Derived : public Base { public: virtual ~Derived() {} };
>
>void intrusive_ptr_add_ref(Base *base) {}
>void intrusive_ptr_add_ref(Base *base) {}
>
>intrusive_ptr<Derived> makeDerived() {
>     return intrusive_ptr<Derived>(new Derived());
>}
>
>string takesBasePtr(intrusive_ptr<Base> const &base) {
>     return "takesBasePtr";
>}
>
>BOOST_PYTHON_MODULE(foo) {
>     class_<Base, intrusive_ptr<Base> >("Base", no_init);
>     class_<Derived, intrusive_ptr<Derived>, bases<Base> >
>         ("Derived", no_init)
>         .def("__init__", make_constructor(makeDerived))
>         .def("takesBasePtr", takesBasePtr)
>     ;
>}
>
>==================================================
>
> >>> from foo import *
> >>> d = Derived()
> >>> d.takesBasePtr()
>Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>Boost.Python.ArgumentError: Python argument types in
>     Derived.takesBasePtr(Derived)
>did not match C++ signature:
>     takesBasePtr(boost::intrusive_ptr<Base>)



More information about the Cplusplus-sig mailing list