Virtual Methods + default arguments
Hi! Is it possible to indicate default arguments for virtual methods, using the BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro? I have this code: struct X { virtual void f(int x = 10, char c = 'a') { std::cout << x << ", " << c << std::endl; } }; void call_f(X& x) { x.f(); } BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(f_overloads, f, 0, 2) struct XWrap: X { XWrap(PyObject* self_): self(self_) {} XWrap(PyObject* self_, const X&): self(self_) {} PyObject* self; void f(int x = 10, char c = 'a') { call_method<void>(self, "f", x, c); } void default_f(int x = 10, char c = 'a') { X::f(x, c); } }; BOOST_PYTHON_MODULE(test) { def("call_f", &call_f); class_< X, XWrap >("X") .def("f", &X::f, &XWrap::default_f, f_overloads()) ; } But this does not compile. Here's the first line of the output of the compiler: D:\Programming\Libraries\boost-cvs\boost\boost/python/detail/caller.hpp(53): error: class "f_overloads" has no member "result_converter" So I tried to change the class_ definition to this: class_< X, XWrap >("X") .def("f", &X::f, &XWrap::default_f) .def("f", &X::f, f_overloads()) ; It compiles fine, but in python:
from test import * x = X() x.f(10, 'c') Traceback (most recent call last): File "<stdin>", line 1, in ? RuntimeError: unidentifiable C++ exception
I also tried to change the second .def to: .def("f", &XWrap::f, f_overloads()) and: .def("f", &XWrap::default_f, f_overloads()) But with the same results. So, is it possible to have virtual methods with default arguments? If so, how? Any pointer would be greatly appreciated. Thanks in advance, Nicodemus.
Bruno da Silva de Oliveira <bso@inf.ufsc.br> writes:
Hi!
Is it possible to indicate default arguments for virtual methods, using the BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro?
<snip>
I also tried to change the second .def to:
.def("f", &XWrap::f, f_overloads())
and:
.def("f", &XWrap::default_f, f_overloads())
But with the same results.
This one is on the right track, but you need a separate version of f_overloads for the second one. The 2nd argument to def in this case just carries type information; it could be a null function pointer for all Boost.Python cares. It's the 3rd arg which carries all the info about what functions to call. -- Dave Abrahams Boost Consulting www.boost-consulting.com
David Abrahams wrote:
Bruno da Silva de Oliveira <bso@inf.ufsc.br> writes:
Hi!
Is it possible to indicate default arguments for virtual methods, using the BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro?
<snip>
I also tried to change the second .def to:
.def("f", &XWrap::f, f_overloads())
and:
.def("f", &XWrap::default_f, f_overloads())
But with the same results.
This one is on the right track, but you need a separate version of f_overloads for the second one. The 2nd argument to def in this case just carries type information; it could be a null function pointer for all Boost.Python cares. It's the 3rd arg which carries all the info about what functions to call.
Nice. The solution is define the overloads as: BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(f_overloads, default_f, 0, 2) generate the module as: class_< X, XWrap >("X") .def("f", &X::f, &XWrap::default_f ) .def("f", &XWrap::default_f, f_overloads()) ; Thanks a lot Dave! (Don't you sleep? 8P) Farewell, Nicodemus.
participants (3)
-
Bruno da Silva de Oliveira -
David Abrahams -
Nicodemus