[C++-sig] std::auto_ptr<> in constructor

Roman Yakovenko roman.yakovenko at gmail.com
Thu Apr 15 08:13:25 CEST 2010

On Thu, Apr 15, 2010 at 7:17 AM, Roman Yakovenko
<roman.yakovenko at gmail.com> wrote:
> On Thu, Apr 15, 2010 at 1:08 AM, Tomasz Mikolajczyk
> <tmmikolajczyk at gmail.com> wrote:
>> Hi,
>> I'm a new user of boost::python art. I started to play a bit with the
>> code and the py++ generator and encountered difficulties with the
>> std::auto_ptr in constructor.
>> ...
>> I understand the error but I don't know what to do to avoid it.

I believe this error has nothing to do with py++ and it is a pure
Boost.Python issue.

I can propose a work around for you:

introduce the following function:

std::auto_ptr< B > createB(int value, std::auto_ptr<A> a){
    return std::auto_ptr< B >( new B( value, a ) );

and instruct py++ to use it as "__init__" method:

B = mb.class_( 'B' )
B.add_fake_constructors( mb.free_function( 'createB' ) )

one more step:

A = mb.class_('A' )
A.held_type = 'std::auto_ptr< %s >' % A.decl_string

This will allow you to create A class instance in Python, and
internally it will be held using auto_ptr class.

I actually tested it and it works (
http://pygccxml.svn.sourceforge.net/pygccxml/?rev=1837&view=rev ).


Roman Yakovenko
C++ Python language binding

More information about the Cplusplus-sig mailing list