Re: Containers for derived classes
Daniel Wallin <[email protected]>
| Newsgroups | gmane.comp.lib.boost.langbinding |
|---|---|
| Message-ID | <[email protected]> |
This is a reply to a post on C++Sig.. At 18:07 2003-08-08, David Abrahams wrote: >[email protected] writes: > > > I.e. the identity is not preserved. Could this be a problem with MinGW, > > which I am using? > > To be sure, here is the complete code again: > > > > #include <boost/shared_ptr.hpp> > > #include <boost/python.hpp> > > > > using namespace boost; > > > > class A { > > public: > > A(int i) : val(i) { } > > int val; > > }; > > typedef shared_ptr<A> A_ptr; > > > > class C { > > public: > > void set(A_ptr a) { this->a=a; } > > A_ptr get() { return this->a; } > > void f() { a.get()->val *= 2; } > > private: > > A_ptr a; > > }; > > > > BOOST_PYTHON_MODULE(ptr_test) > > { > > using namespace boost::python; > > class_<A, A_ptr>("A", init< int >() ) > > .def_readwrite("val", &A::val) > > ; > > class_<C>("C" ) > > .def("set", &C::set ) > > .def("get", &C::get ) > > .def("f", &C::f ) > > ; > > } > >OK, I understand the problem now. It's not a MinGW issue. The >problem is that when you ask for A to be held by A_ptr, an A_ptr >lvalue converter gets registered, since there is actually an A_ptr >object inside the Python wrapper. Lvalue converters can be used where >only an rvalue is needed, as in: > > void set(A_ptr a) > >and they get special priority over rvalue converters. The only way to >get the behavior you want is actually to NOT hold A objects by A_ptr: > > class<A>("A", init<int>() ) > ... > >so that the rvalue converter that gets registered can be found. > >Clearly, we need special rules for shared_ptr, so that this won't >happen. I'm just not sure whether we need special rules for all >smart pointer types, yet. Any thoughts? I don't think any other smart pointer types need this special handling, although I guess it could simply be solved more generally by introducing a real priority value for converters. IIRC boost.python already has a specialization for C++ -> Python conversions of shared_ptr's, so introducing one for Python -> C++ would also solve this right? --- Daniel Wallin ------------------------------------------------------- This SF.Net email sponsored by: Free pre-built ASP.NET sites including Data Reports, E-commerce, Portals, and Forums are available now. Download today and enter to win an XBOX or Visual Studio .NET. http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01