[C++-sig] Re: New-style polymorphism

David Abrahams dave at boost-consulting.com
Wed Jul 28 17:40:05 CEST 2004


David Abrahams <dave at boost-consulting.com> writes:

> Daniel Wallin <dalwan01 at student.umu.se> writes:
>
>> David Abrahams wrote:
>>> 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.
>>
>> Here's a problem: Trying to call a base class member function statically
>> from python will call the virtual function, resulting in infinite
>> recursion.
>
> #*&!!!@@
>
> How could I miss that one?  And how did it ever work, even with the
> existing mechanism?
>
> I'll look into this, but I'm not optimistic it can be fixed :(.

Actually, the solution is to keep using the same way of exposing the
default method implementation: write a default_f member function that
calls the default implementation and expose it during wrapping.  That
removes one of the advantages, leaving us with this list:

  * 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 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* !

  * A C++ pointer or reference to a polymorphic T created from
    Python (one that is actually also a wrapper<T>) can be
    converted back to Python without creating a new Python object.

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




More information about the Cplusplus-sig mailing list