[C++-sig] New-style polymorphism

David Abrahams dave at boost-consulting.com
Mon Jul 26 17:43:37 CEST 2004


I just checked in changes that improve the way we can wrap classes
with virtual functions in Boost.Python.  This is essentially what's
described in
http://www.boost.org/libs/python/todo.html#base-class-for-virtual-function-callback-wrappers


The differences are most apparent if you compare
libs/python/test/polymorphism.cpp and
libs/python/test/polymorphism2.cpp.

  * you no longer need an initial "self" argument to yourconstructor.
    Instead you derive your class X from wrapper<X> (I'm looking for
    better names for that base class)

  * You no longer need to define a special "default" member function
    for the default behavior, and you only have to pass one member
    function pointer to "def()"

  * You no longer need to use call_method; you instead ask the
    wrapper<X> base class for the method override, and then call that
    instead.  If there's no override, you call the default directly.
    On some broken compilers you may need to use
    call<result_type>(...) args for some of the calls (always on
    vc6/7, for reference return types only on some other compielrs).
    Most other reasonably conforming compilers will convert the result
    of calling the override directly into the function's return type.

  * It should be much faster in the case where you have a default
    implementation that isn't overridden in Python, because you'll
    usually not have to enter the Python interpreter.

  * Many problems with dangling references have been eliminated,
    because you don't have to take a round-trip through Python.  You
    can wrap virtual functions that return char const* !

I hope to get this documented before the release, but in case I
don't, the old method still works; I'd like people to get some
experience with this new feature though, so I can see if it's working
out as it should.  It would be great if Pyste could be switched to
generate the appropriate code.

Thanks,
-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list