[C++-sig] operator+(A, B) without B::B() default constructor in boost.python

Sebastian Walter walter at mathematik.hu-berlin.de
Mon May 18 10:17:24 CEST 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

That, you will have to ask the devs.
The question is however:
what is quicker: wrapping 20 functions manually with copy/paste or trying to understand boost::python resp. wait for an answer from the devs.
It is my impression, that you have to know a lot about boost::python to understand why something does not work as it should.

If someone knows the answer, I'd be also be interested, of course ;)




Hans Roessler schrieb:
> Thank you for suggesting the workaround. But I would like to understand _why_ the operator overloading doesn't work the Boost.Python way in my example (or what I have done wrong).
> It can be quite tedious to write a wrapper like __add__ for each operator and pair of classes, and I thought the ".def(self <op> other)" syntax was implemented for exactly this purpose?
> 
> Hans
> 
> 
> 
> ----- Ursprüngliche Mail ----
>> Von: Sebastian Walter <walter at mathematik.hu-berlin.de>
>> An: Development of Python/C++ integration <cplusplus-sig at python.org>
>> Gesendet: Freitag, den 15. Mai 2009, 11:02:23 Uhr
>> Betreff: Re: [C++-sig] operator+(A, B) without B::B() default constructor in boost.python
>>
> Hans Roessler schrieb:
> 
> Hello Hans,
> 
> in my experience .def(self+other) often didn't quite do what I expected.
> You could try something like that:
> 
> int int__add__A_B(const A &lhs, const B &rhs){
>      return operator+(A,B);
> }
> 
> .def("__add__", &int__add__A_B)
> 
> 
> i.e. first define a non-overloaded function int_add__A_B and then bind it to 
> __add__ (which is the Python operator+ function).
> 
> regards,
> Sebastian
> 
> 
>>>> Hello,
>>>> how can I wrap the following overloaded operator+ using boost.python?
>>>> The code as is fails with 
>>>>> ...
>>>>> pytest.cpp: In function void init_module_pytest():
>>>>> pytest.cpp:19: error: expected primary-expression before ( token
>>>>> ...
>>>> If I try ".def(self+B())" instead of ".def(self+other())", the error is
>>>>> ...
>>>>> pytest.cpp: In function void init_module_pytest():
>>>>> pytest.cpp:17: error: no matching function for call to B::B()
>>>>> ...
>>>> best wishes
>>>> Hans
>>>>
>>>>
>>>> pytest.cpp:
>>>>
>>>> #include 
>>>> using namespace boost::python;
>>>>
>>>> class A{
>>>> public:
>>>> };
>>>> class B{
>>>> public:
>>>>   B(int i){};
>>>> };
>>>>
>>>> int operator+(const A& a,const B& b){return 42;};
>>>>
>>>> BOOST_PYTHON_MODULE(pytest)
>>>> {
>>>>   class_("A")
>>>>     .def(self+other())   //.def(self+B())            //line 17
>>>>     ;
>>>>   class_("B",init)    //line 19
>>>>     ;
>>>> }
>>>>
>>>>
>>>>      
>>>> _______________________________________________
>>>> Cplusplus-sig mailing list
>>>> Cplusplus-sig at python.org
>>>> http://mail.python.org/mailman/listinfo/cplusplus-sig
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig at python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFKERmU9PBA5IG0h0ARAgFMAKCDx+DGod3m8c+nE4auuR0+dZjtIQCeIXHf
S6X++I+Ilw74MqtKzvYWiCo=
=dcH2
-----END PGP SIGNATURE-----


More information about the Cplusplus-sig mailing list