Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2003-01-15 00:16:13


"Bruce Lowery <bruce_lowery_at_[hidden]>" <bruce_lowery_at_[hidden]> writes:

> <bruce_lowery_at_y...>" <bruce_lowery_at_y...> wrote:
>>
>> Using Boost/Python, how does one overload methods? For instance:
>>
>> struct Bar
>> {
>> void func( const std::string& s );
>> void func( int i );
>> };
>>
>> BOOST_PYTHON_MODULE( Foo )
>> {
>> class_<Bar>( "Bar" )
>> .def( "func", &Bar::func )
>> .def( ??? )
>> ;
>> }
>>
>> The reference documentation discusses the
>> BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro, but it only seems to
>> apply to methods with default arguments.
>>
>> --bruce
>
>
> I should have scanned the python c++ SIG first. Also now realize
> that python.boost questions are preferred on that list rather than
> here.
>
> For future newbie reference, solution to above appears to be:
>
> struct Bar
> {
> void func( const std::string& s );
> void func( int i );
> };
>
>
> BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS( Bar_func_overloads, Bar::func, 1, 1 )
>
>
> BOOST_PYTHON_MODULE( Foo )
> {
> class_<Bar>( "Bar" )
> .def("func",(void(Bar::*)(const std::string&))0,Bar_func_overloads)
> .def("func",(void(Bar::*)(int))0,Bar_func_overloads)
> ;
> }

This is not really a good use of
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS. You should leave it out and do
this instead:

 BOOST_PYTHON_MODULE( Foo )
 {
    class_<Bar>( "Bar" )
      .def("func"
           , static_cast<void(Bar::*)(const std::string&)>(&Bar::func))
      .def("func"
           , static_cast<void(Bar::*)(int)>(&Bar::func))
      ;
 }

-- 
                       David Abrahams
   dave_at_[hidden] * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net