[C++-sig] shared_ptr converters

Hamilton Feltman hfeltman at vbbn.com
Tue Apr 15 08:18:19 CEST 2003


Hello! So I'm not the only one stumbling on this. Unfortunately I don't
have an answer, but I boiled down the code to demonstrate the problem:


struct SomeClass
{
	int a, b;
	SomeClass (int a, int b) : a(a), b(b) {}
};

boost::shared_ptr<SomeClass> SomeFunction (int a, int b)
{
	return boost::shared_ptr<SomeClass> (new SomeClass(a, b));
}

BOOST_PYTHON_MODULE(Test)
{
	class_<SomeClass>("SomeClass", init<int, int>());
	def ("SomeFunction", SomeFunction);
}

>>> from Test import *
import Test # dynamically loaded from Test_d.dll
[9005 refs]
>>> obj = SomeFunction (3,3)
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 SomeClass>
[9021 refs]

Anyone have a clue? Seems like a fairly straightforward conversion, but
I don't know enough about boost python, or all the internal amazements
going on inside it (but learning quick). I'm running vc7 on windows.

Hamilton Feltman
Harmonicdog Entertainment


-----Original Message-----
From: c++-sig-admin at python.org [mailto:c++-sig-admin at python.org] On
Behalf Of Nicodemus
Sent: Monday, April 14, 2003 4:53 PM
To: c++-sig at python.org
Subject: [C++-sig] shared_ptr converters

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.




_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig





More information about the Cplusplus-sig mailing list