On 04/10/2012 06:58 AM, Holger Brandsmeier wrote:
Dear list,

I exported a wrapper for std::complex<double>  to python and I am
willing to always use this class in python and I never want to use
pythons own `complex` data type.

However, a funny thing happens when I export the addition operator
(the implemenation is given in the STL ...)
         .def(self + self)

Now I get that:
type(scalar_cd(1,2) + scalar_cd(1,2))
<type 'complex'>

But my own type is of the form:
type(scalar_cd(1,2))
<class 'parfem.scalarPy.scalar_cd'>


Why did this happen and how can I avoid this? Later own I get problems
when I want to pass this back to C++.

I remember that pythons own complex type is a bit special and does not
cooperate nicely with boost::python and std::complex<>  but how did
python/boost::python end up converting the result of the addition to
`complex`?

I am using:
Python 2.7.2
boost-1.46.1


Does your custom complex type provide implicit conversion to std::complex?

If so, I'm guessing there's something going wrong with overload resolution; Boost.Python's operator wrappers simply invoke the C++ operators and lookup the Python conversion based on the result type. You can test this yourself with something like this:

--------------------------------------------------------------------

// use any namespace other than the one your classes are defined in
namespace {

template <typename T> void test_expr(T const & x) {
    std::cerr << typeid(x).name() << std::endl;
}

} // <anonymous>

int main() {
    test_expr(whatever::scalar_cd(1,2) + whatever::scalar_cd(1,2));
    return 0;
}

--------------------------------------------------------------------

If the output you see when running the above is std::complex, you'll have a test case that doesn't involve Boost.Python you can use to debug your overload resolution. If not, let me know, and I'll dig deeper into what Boost.Python's doing.

HTH!

Jim
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to