boost python v2: error with both default arg and pointer-return function
Hi, When using the boost python v2 to port a C++ member function like the following, I got an error with message "TypeError: bad argument type for built-in operation". class A; A* MyClass::foo(int n = 100); This only happens when a member function a) has a default argument; b) returns a pointer value. Here is my related C++ code: ---------------------------------------------- class Number { public: Number(int n); Number* copy(int n = 100); }; Number* Number::copy(int n) { printf("number of copies = %d\n", n); return this; } BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(copy_overloads, copy, 0, 1); BOOST_PYTHON_MODULE(numbermod) { python::class_<Number> number_class("Number", python::init<int>()); number_class.def("copy", &Number::copy, python::return_internal_reference<>(), copy_overloads()); } ---------------------------------------------- python code: ---------------------------------------- from numbermod import * n = Number(10) n1 = n.copy() ---------------------------------------- error message: ---------------------------------------- Traceback (most recent call last): File "./test.py", line 19, in ? n1 = n.copy() TypeError: bad argument type for built-in operation Can anyone please tell me what's wrong? Thanks. Liwei __________________________________________________ Do you Yahoo!? Y! Web Hosting - Let the expert host your web site http://webhosting.yahoo.com/
Liwei Peng <liwei_peng@yahoo.com> writes:
Hi,
When using the boost python v2 to port a C++ member function like the following, I got an error with message "TypeError: bad argument type for built-in operation".
class A; A* MyClass::foo(int n = 100);
This only happens when a member function a) has a default argument; b) returns a pointer value.
Can anyone please tell me what's wrong?
It has nothing to do with the pointer value. If you want to wrap functions with default arguments so that you can omit them when called from Python, you have to remember that the defaults are not part of the type of the function pointer, and use the techniques described in: http://www.boost.org/libs/python/doc/tutorial/doc/default_arguments.html and http://www.boost.org/libs/python/doc/v2/overloads.html HTH, -- David Abrahams dave@boost-consulting.com * http://www.boost-consulting.com
participants (2)
-
David Abrahams -
Liwei Peng