[C++-sig] Re: boost-python + lambda

Neal D. Becker ndbecker2 at verizon.net
Wed Apr 7 20:09:19 CEST 2004


Stefan Seefeld wrote:

> Neal D. Becker wrote:
>> I'd like to wrap a class, but with a modified interface to some member
>> functions.  The easy way is write c++ code to wrap the class, inherit
>> from it, and add the new member function, then expose that.
> 
> There is no need to derive a new class just to be able to wrap an
> individual method. A function with the right signature will do just fine:
> 
> struct A
> {
>    void method(int i);
> };
> 
> void method_wrapper(A &a, int i)
> {
>    std::cout << "before the call" << std::endl;
>    a.method(i);
>    std::cout << "after the call" << std::endl;
> }
> 
> ...
> 
> class_<A> a("A");
> a.def("method", method_wrapper);
> 
> 
> That's not quite as compact as a lambda but much less overhead than
> a derived class. I believe the only requirement on the wrapper signature
> is that the first argument is convertible to an 'A' reference.
> 

Thanks!  (Now why didn't I think of that? :)





More information about the Cplusplus-sig mailing list