[C++-sig] shared_ptr converters

Nicodemus nicodemus at globalite.com.br
Tue Apr 15 01:52:32 CEST 2003


Hi all!

I'm having some troubles with the boost::shared_ptr support. Suppose 
this code:

test.h -------------------------------------------------------

    #include <boost/shared_ptr.hpp>

    struct A
    {
        virtual int f() = 0;
    };

    struct B: A
    {
        int f() { return 1; }
    };

    boost::shared_ptr<A> New()
    {
        return boost::shared_ptr<A>( new B() );
    }


test.cpp -------------------------------------------------------

    // Includes
    ====================================================================
    #include <boost/python.hpp>
    #include <test.h>

    // Using
    =======================================================================
    using namespace boost::python;

    // Declarations
    ================================================================

    struct A_Wrapper: A
    {
        A_Wrapper(PyObject* self_):
            A(), self(self_) {}

        int f() {
            return call_method< int >(self, "f");
        }

        PyObject* self;
    };


    // Module
    ======================================================================
    BOOST_PYTHON_MODULE(test)
    {
        class_<A, A_Wrapper, boost::noncopyable, boost::shared_ptr<A> >("A")
        ;

        def("New", &New);
    } 

The code compiles fine, without any errors or warnings. Now, inside Python:

ActivePython 2.2.2 Build 224 (ActiveState Corp.) based on
Python 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>>
 >>> from test import *
 >>> New()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: No to_python (by-value) converter found for C++ type: class 
boost::shared_ptr<struct A>
 >>>

Is this a bug, or am I missing something? I tried changing the order of 
the template parameters of class_, but then the code either doesn't 
compile, or compiles but gives the same results (I don't think the error 
messages from the compiler are revelant in this case, but I will post if 
there's need).

Regards,
Nicodemus.







More information about the Cplusplus-sig mailing list