[C++-sig] weakref to shared_ptr conversion

Andreas pirx at cs.tu-berlin.de
Tue Jan 10 22:51:37 CET 2006


hello,

I have a problem with implicit conversion of weakref of shared_ptr to 
shared_ptr.

I have the following code:
//c++
class A
{
   public:
     static shared_ptr<A> create() {
       return shared_ptr<A>(new A());
     };
     ~A() {}

   private:
     A() {}
};

void foo(shared_ptr<A> pA)
{
}

BOOST_PYTHON_MODULE(Test)
{
   class_<A, shared_ptr<A> >("A", no_init)
     .def("create", &A::create)
     .staticmethod("create")
   ;
   boost::python::def("foo", foo);
}

//python
import weakref
a = A.create()
weak_a = weakref.ref(a)

foo(a)  #works fine
foo(weak_a)
#error:
#    foo(weak_a)
#Boost.Python.ArgumentError: Python argument types in
#    CodeSubWars_Common.foo(weakref)
#did not match C++ signature:
#    foo(class boost::shared_ptr<class A>)


How do i get such an implicit conversion from weakref to shared_ptr 
working? Is it possible to define an implicit converter to solve this?
(I'm using boost 1.33.1, MSVC7.1 and Python 2.4.2)

Any help would be appreciated,
andreas rose




More information about the Cplusplus-sig mailing list