[C++-sig] Boost python exposing abstract class , function returning boost::shared_ptr

warin xavier.warin at gmail.com
Fri Mar 27 14:27:11 CET 2015


> I'm not top posting.

I have an abstract class A2, with derived class B2. 
I want to map a function returning a boost::shared_ptr of A.
This abstract class is wrapped :

struct A2{ 
  virtual ~A2(){}
  virtual int ret() =0;
 };

struct B2: public A2{ 
virtual ~B2(){}
  int ret() { return 1; }
}; 

// wrapper
struct  A2Wrap : A2, wrapper<A2>
{
  inline int ret() 
  {
     return this->get_override("ret")();
  }
};

// function to map
boost::shared_ptr<A2>  f1()
  {
    return  boost::shared_ptr<A2>(new B2());
   }

 // expose to python
BOOST_PYTHON_MODULE(grids)
 {
    class_<A2Wrap, boost::shared_ptr<A2Wrap> , boost::noncopyable >("A2")
   .def("ret",pure_virtual(&A2::ret));

    class_<B2,boost::shared_ptr<B2>,bases<A2>>("B2"); 
    def("f1",f1); 
 }  


I get the following result on execution :

>import grids
>>> a = grids.f1()

Traceback (most recent call last):

File "<stdin>", line 1, in <module>
TypeError: No to_python (by-value) converter found for C++ type: 
boost::shared_ptr<A2>

Any idea for this problem ? I imagine to express the conversion from 
boost::shared_ptr<A2> to boost::shared_ptr<A2Wrap>.




More information about the Cplusplus-sig mailing list