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

Stefan Seefeld seefeld at sympatico.ca
Wed Apr 7 19:55:45 CEST 2004


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.

Regards,
		Stefan








More information about the Cplusplus-sig mailing list