[C++-sig] strange behavior with respect to numeric and Boolean overloads

Matthew Scouten (TT) Matthew.Scouten at tradingtechnologies.com
Mon Mar 16 16:28:04 CET 2009


So here is some strange behavior I ran across with BP. A take a look at
this example code: 

 

busybox.cpp:

 

#include "stdafx.h"

 

std::string foo_int   (int    arg) { return std::string("foo(int)
Called"); }

std::string foo_double(double arg) { return std::string("foo(double)
Called"); }

std::string foo_bool  (bool   arg) { return std::string("foo(bool)
Called"); }

BOOST_PYTHON_MODULE(busybox)

{

    bp::def("foo", foo_double);

    bp::def("foo", foo_bool);

    bp::def("foo", foo_int);

    

}

 

python Interpreter:   

 

Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import busybox

>>> busybox.foo(5.0)

'foo(double) Called'

>>> busybox.foo(5)

'foo(int)    Called'

>>> busybox.foo(True)

'foo(int)    Called'

>>> 

 

Note that the int overload is called for a bool argument. If I change
the order of the defs in the cpp file I can effect this to some extent.
If I put the def for the bool overload last, IT gets called for both int
and bool parameters. If I put the double override last, that override
gets called for ALL 3 CALLS regardless of what is passed.

 

Now the order sensitivity is annoying, but I can figure out how to make
it work right, and then comment the dickens out of the code. The real
killer is that there does not seem to be any way to force BP to
differentiate between a bool and an int. The only thing that has worked
is to apply a name skew (ie: bp::def("foo_bool", foo_bool);). This
works, but the users carp at me for changing the interface from the
underlying library.

 

Is there a better work around?

Is this a bug?    

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20090316/8b63ae1c/attachment.htm>


More information about the Cplusplus-sig mailing list