Passing a lambda into a function template

Your function binsearch takes a function pointer as argument. A lambda and a function pointer are different types: a lambda may be considered as an instance of a struct implementing operator(). Note that stateless lambdas (lambdas that don’t capture any variable) are implicitly convertible to function pointer. Here the implicit conversion doesn’t work because of … Read more

How do I compare two functions for pointer equality in the latest Go weekly?

Note that there is a difference between equality and identity. The operators == and != in Go1 are comparing the values for equivalence (except when comparing channels), not for identity. Because these operators are trying not to mix equality and identity, Go1 is more consistent than pre-Go1 in this respect. Function equality is different from … Read more

How Can I Pass a Member Function to a Function Pointer?

You can’t. You either pass a pointer to a static method or Parent has to accept also a pointer to the object. You might want to look at boost::bind and boost::function for that: #include <boost/bind.hpp> #include <boost/function.hpp> struct Y { void say(void) { std::cout << “hallo!”;} boost::function<void()> getFunc() { return boost::bind(&Y::say, this); } }; struct … Read more

Dynamic method dispatching in C

As others have noted, it is certainly possible to implement this in C. Not only is it possible, it is a fairly common mechanism. The most commonly used example is probably the file descriptor interface in UNIX. A read() call on a file descriptor will dispatch to a read function specific to the device or … Read more