[C++-sig] [Py++] Nested enum as default arg

Kirill Lapshin kir at lapshin.net
Wed Sep 6 21:20:02 CEST 2006


Roman Yakovenko wrote:
>> struct Foo
>> {
>>      enum Bar {BAR1, BAR2};
>>
>>      explicit Foo(Bar arg = BAR1);
>> };
>>
>>
>> Py++ generates code that tries to add constructor (and default argument)
>> prior to enum being registered. Result is exception in module import.
>>
>> Any ideas how to fix / work around? Other then moving enum outside the
>> class?
> 
> mb = module_builder_t( ... )
> Foo = mb.constructor( 'Foo' ) #I assume that there is only one constructor
> Foo.arguments[0].default_value = 'int(%s)' % Foo.arguments[0].default_value
> 

Oh, I see. But that would only work for enums, wouldn't it? I would 
prefer some more generic solution. In fact I've already encountered 
another similar problem. This time it is more along the lines of:

struct Foo
{
     explicit Foo( const shared_ptr<Foo>& foo = shared_ptr<Foo>() );
};


> You are not the first one who has this problem. Another work around is:
> using boost::python;
> class_< Foo > foo( "Foo", no_init );
> scope foo_scope( foo );
> enum_< ... >...;
> foo_scope.def( init< ... >( ... ) );

That makes a lot of sense, but that requires modification of Py++, 
doesn't it? Or is there a way to customize code generation to do it the 
way you've cited?

Seems to me if Py++ would always opt for code like that, it would 
already solve quite a few dependency problems.

I would be more then happy to implement this. Could you point me in 
right direction, please?

> If you search this mailing list you will find few discussions about
> this problem.
> I hope I will be able to submit patch to the library that will solve "default
> arguments" dependency problem.

Yep, I've found few messages. Looks like you are planning to patch 
boost.python. That would be the ultimate solution! Though it is a long 
term solution, and we also need something in short/mid term...

Kirill




More information about the Cplusplus-sig mailing list