Using Boost Python & std::shared_ptr

前端 未结 4 2128
悲哀的现实
悲哀的现实 2021-02-13 06:30

I\'m trying to get Boost Python to play nicely with std::shared_ptr. Currently, I\'m receiving this error:

Traceback (most recent call last):
  File \"test.py\",         


        
4条回答
  •  长情又很酷
    2021-02-13 07:10

    A snippet from http://boost.2283326.n4.nabble.com/No-automatic-upcasting-with-std-shared-ptr-in-function-calls-td4573165.html:

    /* make boost::python understand std::shared_ptr */
    namespace boost {
           template
           T *get_pointer(std::shared_ptr p)
           {
                   return p.get();
           }
    }
    

    Worked for me. You would define your classes like:

    class_>("Foo", ...);
    

    With this, other methods returning std::shared_ptr will Just Work.

    Some magic may be required for virtual functions/polymorphism, that should be covered in the thread I linked to.

提交回复
热议问题