[pygccxml-development] Proper overload resolution
Brought to you by:
mbaas,
roman_yakovenko
|
From: Benoit L. <ben...@mo...> - 2010-05-17 15:23:50
|
Hello,
Consider the following class:
struct MyClass
{
void setBool(bool a);
void setInt(int a);
};
Because of the way the overloading resolution works currently in boost
python, if you're trying to bind these two functions to __setitem__,
only one of these functions (the last one bound actually) will always be
called, no matter if you call __setitem__ with a bool or an int (for
more information of the why, please take a look at the cplusplus-sig
thread mentioned below).
The only way I could find to bind these two methods to __setitem__ and
have them called "correctly" is to bind them as two separate functions,
and then define a __setitem__ function in python at runtime, by adding
something like that in the __init__.py file:
def custom_set_item(a):
if isinstanceof(a, bool):
...
...
MyClass.__setitem__ = custom_set_item
Maybe there's a way to do that by binding a setitem which would take a
bp::object and test the type in C++, I haven't tried.
It looks like Troy D. Straszheim came with a better implementation of
the overloading mechanism (which is much closer to what we expect):
http://mail.python.org/pipermail/cplusplus-sig/2009-December/015047.html
It is apparently considered for addition in boost, but looks like
they're waiting for the new langbinding integration:
http://www.boost.org/doc/libs/1_43_0/libs/python/todo.html
Any chance one of you have tried Troy's modification, or have any
suggestion for this problem?
Thanks!
Benoit
|