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

Matthew Scouten (TT) Matthew.Scouten at tradingtechnologies.com
Mon Mar 16 19:30:35 CET 2009


I read the linked page, but this still feels like a bug to me. Or at least a wart. Like I said, the order sensitivity I can live with, because that only effects me. But the int vs bool problem is really obnoxious. C++ can tell an int from a bool, Python can tell an int from a bool, but library between them is discarding this information. 

I agree that the best solution (from a purely technical view point) would be to rename the functions in the underlying c++ library, but I do not have control over that library and they have good reason to serve c++ users first and to resist interface breaking changes. From a UI viewpoint, the function is called foo. It foos the data that you pass to it. Having functions foo_int and foo_bool is not good style in either c++ or python.

The problem seems to be that a bool implicitly cast to an int AND an int implicitly cast to a bool. If one of these could be blocked, then there would be some order that would make things work.   

-----Original Message-----
From: cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at python.org [mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at python.org] On Behalf Of Roman Yakovenko
Sent: Monday, March 16, 2009 12:00 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] strange behavior with respect to numeric and Booleanoverloads

2009/3/16 Matthew Scouten (TT) <Matthew.Scouten at tradingtechnologies.com>:
> 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);
>
>
>
> }
>
> ...
>
> 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?

Yes:

1. rename your functions

2. introduce a function in Python, which selects "the right" function,
based on argument type.

> Is this a bug?

Take a look on the following doc:
http://www.language-binding.net/pyplusplus/documentation/functions/registration_order.html

HTH


-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig at python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


More information about the Cplusplus-sig mailing list