[C++-sig] C++ clas with (*args, **kwargs) constructor

Trigve Siver trigves at yahoo.com
Fri Jun 22 00:15:57 CEST 2012


> From: Holger Brandsmeier <brandsmeier at gmx.de>
> To: Trigve Siver <trigves at yahoo.com>; Development of Python/C++ integration <cplusplus-sig at python.org>
> Cc: 
> Sent: Thursday, June 21, 2012 11:58 PM
> Subject: Re: [C++-sig] C++ clas with (*args, **kwargs) constructor
> 
>T rigve,
> 
> with the line
>   
> class_<Form, boost::noncopyable, boost::shared_ptr<Form>>("Form") 
> ;
> you declare Form to have a default constructor (you didn't use any
> init<> struct. That is why you can not pass anything to the
> constructor. If you want to use a constructor with a single (but
> arbitrary) type, then you should use PyObject on the C++ side. Try to
> get this working and tell us if this is how you want it to work, I
> have a feeling that you already might not like having to manually
> convert the PyObject* to the proper types. However, you can not avoid
> it, you can not expect boost::python to automatically call a
> constructor taking a single `int` if all that you export is a
> constructor taking one `PyObject`, you will have to do the proper
> conversions on the C++ side.
> 
> Second if you want to work with many objects of type PyObject* or even
> `kwargs`, then
>   http://wiki.python.org/moin/boost.python/HowTo#A.22Raw.22_constructor
> is one option. I didn't really do this myself, but what is your problem 
> there?

Thanks for reply :)

Ok it looks like it is working now :) When I used raw_constructor I have set min arguments to 2, like this:

...

boost::shared_ptr<Form> create(tuple, dict)
{
    return boost::shared_ptr<Form>(new Form());
}

class_<Form, boost::noncopyable, boost::shared_ptr<Form>>("Form", no_init)
        .def("__init__", raw_constructor(&create, 2))

...
But when *args or **kwargs was empty (or both of them),  it was spoiling errors. So I set min arguments to 0 (removed the parameter respectively). Hope I'm doing it right way.

>  ...

> 
> -Holger
> 

Thanks

Trigve



More information about the Cplusplus-sig mailing list