[C++-sig] Re: Boost.Python not resolving overloads

David Abrahams dave at boost-consulting.com
Sun Sep 21 04:10:37 CEST 2003

"Niall Douglas" <s_sourceforge at nedprod.com> writes:

> On 20 Sep 2003 at 21:46, David Abrahams wrote:
>> >>>> from TnFOX import *
>> >>>> fh=FXFile("fred.txt")
>> >>>> print fh.name()
>> > Traceback (most recent call last):
>> >   File "<stdin>", line 1, in ?
>> > Boost.Python.ArgumentError: Python argument types in
>> >     FXFile.name()
>> > did not match C++ signature:
>> >     name(class FX::FXString)
>> >     name(class FX::FXFile {lvalue})
>> >>>> print fh.name(fh)
>> > fred.txt
>> >
>> > As it says, there are two name()'s defined. 
>> Clearly it's a static method which takes a single argument.  if you
>> want to call it as fh.name() you can't make it static (or you need an
>> overload which takes no arguments).
> Yes sorry - it's 3am here. One of the name()'s is static but the 
> other one isn't:
> class FXFile
> {
>    const FXString &name() const;
>    static FXString name(const FXString &file);
> };

There's no such thing as that in Python.  Overloading is somewhat of
a simulation too.  Any given name can only be static or non-static.

> Now in my example above, I was calling name(void), not name(file). If 
> you call name(fh) then it correctly calls the name(void).
>> > I tried a number of other 
>> > functions with no parameters and all of them cause an error if there
>> > are overloads present. The only way to make them work is to include
>> > the instance as the first parameter.
>> >
>> > Is this right? It's counter-intuitive and inconvenient - surely
>> > boost.python can try inserting the instance if no parameters are
>> > present?
>> Wha???  No, it can't just guess at what you mean.  
> I don't see why. If one of my name()'s is zero parameters and the 
> other one takes a string

Neither is zero parameters, as the error indicates.  One takes an
FXString and the other takes an FXFile.

> , and I call name(), then it's obvious I want 
> the zero parameter one.
> Besides, the error message is from Boost.Python, not Python.

Boost.Python uses the Python staticmethod wrapper to create static

> If this is not known behaviour, I'll make you a demo though it would 
> be trivial for you to write yourself.

a demo from you is always less trivial than making it myself.
In this case, though, the behavior is known and expected.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list