How should I pass a function inside an struct as a functor? I assumed this should work fine, but it didn’t:
#include <algorithm>
using namespace std;
struct s {
int a[10];
bool cmp(int i, int j) {
// return something
}
void init() {
sort(a, a + 10, cmp);
}
};
which gets <unresolved overloaded function type>
You cannot do this directly, because
cmpis a member function, which requires three arguments:i,j, and the invisible, implicitthispointer.To pass
cmptostd::sort, make it a static function, which does not belong to any particular instance ofsand thus doesn’t have athispointer:If you need access to
this, you can wrapcmpin a simple function object instead:And call it like this: